续集ORM MM关系
我正在尝试制作一个实时Messenger应用程序。我正在将MySQL用于数据库,并续集为ORM。我不确定应该如何设置对话的模式。
用户可以进行许多对话,并且对话可以有很多用户(只有2个),因此我假设我选择多对多的关系是正确的。
尝试此操作时,我最终使用一个带有user_id和convents_id的接线表。因此,当对话将插入2行时,每个user_id插入一个。我的问题在于查询某些用户对话。当我这样做时,我还想获取对话中第二用户的用户_ID和其他有关第二用户的详细信息。
目前,我的查询看起来像这样。...
const user_convos = await db.models.conversations.findAll({
include: [{
model: db.models.users,
through: {
where: {userId: uid},
attributes: ['userId']
}
}]
});
我的JSON输出看起来像这样...
{
"id": 2,
"latest_message": "Hello World",
"createdAt": "2022-04-06T00:11:08.000Z",
"updatedAt": "2022-04-06T00:11:08.000Z",
"users": [
{
"id": 1,
"email": "[email protected]",
"username": "john101",
"first_name": "John",
"last_name": "Smith",
"conversation_user": {
"userId": 1
}
}
]
}
如您在JSON对象的用户键中所看到的,我只能在用户上获取查询的数据,但是我也希望对话中其他用户的数据。如上所述,该其他用户将在接线表中有自己的行,因此我最明显的方法是然后查询使用输出中使用“ ID”的所有对话,然后获取其他用户ID,然后查询以查询他们的信息。
尽管这可能会起作用,但从长远来看,这听起来很愉快且不可持续,因为我将进行多个查询。我确定有一种更容易的方法(就像往常一样),我非常感谢任何更轻松地解决此问题的反馈(也许是某种高级查询方法或其他数据库架构...)。
提前致谢!
I am trying to make a real time messenger app. I'm using mysql for my database and Sequelize as the ORM. I am unsure on how I should set up my schema for conversations.
A user can have many conversations, and a conversation can have many users (only 2), and so I assuming my choosing a Many-To-Many relationship is correct.
When trying this I end up with a junction table with a user_id and conversation_id. So when a conversation 2 rows will be inserted, one for each user_id. My issue lies in querying for a certain users conversations. When I do this, I would like to also get the user_id and other details about the second user in the conversation.
Currently, my query looks like this....
const user_convos = await db.models.conversations.findAll({
include: [{
model: db.models.users,
through: {
where: {userId: uid},
attributes: ['userId']
}
}]
});
My json output looks like this...
{
"id": 2,
"latest_message": "Hello World",
"createdAt": "2022-04-06T00:11:08.000Z",
"updatedAt": "2022-04-06T00:11:08.000Z",
"users": [
{
"id": 1,
"email": "[email protected]",
"username": "john101",
"first_name": "John",
"last_name": "Smith",
"conversation_user": {
"userId": 1
}
}
]
}
As you can see in the users key of the json object, I only get the data on the user making the query, but I would also like the data of the other user in the conversation. As aforementioned, this other user will have his own row in the junction table, so I the most obvious approach to me is to then query for all conversations using the 'id' in the output and then get the other users id and then query for their information.
Though this will probably work, this sounds longwinded and unsustainable in the long run since I multiple queries will be made. I am sure there is a much easier approach to this (as there always is), I would very much appreciate any feedback pertaining easier methods to go about this (Maybe some sort of advanced query method or a different database schema...).
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果总是一对一的对话,那么您根本就不需要交界表。只需在对话表中添加两个用户ID列即可。
在这种情况下,协会可能看起来像这样:
续集查询可能看起来像这样:
If it's always a one-to-one conversation then you don't need a junction table at all. Just add two user id columns in the conversation table.
In this case assocaitions might look like this:
And a Sequelize query might look like this: