简体   繁体   English

我是否允许在 Sequelize 中定义用户和服务器模型之间的多对多和一对多关系?

[英]Am I allowed to define both many-to-many and one-to-many relationships between User and Server models in Sequelize?

I have 2 models called User and Server and my verbal description of their relationships is something like this:我有 2 个模型,称为 User 和 Server,我对它们关系的口头描述是这样的:

  1. A User can create Servers and is therefore their owner.用户可以创建服务器,因此是其所有者。 Following this logic, I need to implement a one-to-many relationship between these 2 models.按照这个逻辑,我需要在这两个模型之间实现一对多的关系。 That means the Server should have a userId column which will contain the id of the User/Owner.这意味着服务器应该有一个 userId 列,其中包含用户/所有者的 id。

  2. Users can join many servers and a Server can be joined by many users, so I need to implement many-to-many relationship between these 2 models and have a junction table as well.用户可以加入很多服务器,一个服务器可以被很多用户加入,所以我需要在这两个模型之间实现多对多的关系,并且还要有一个联结表。

So my question is can I apply 2 relationships between the models like this:所以我的问题是我可以像这样在模型之间应用两种关系:

let User = require('./models/User');
let ServerUser = require('./models/ServerUser');
let Server = require('./models/Server');

User.hasMany(Server)
Server.belongsTo(User)

User.belongsToMany(Server, { through: ServerUser });
Server.belongsToMany(User, { through: ServerUser });

What makes me think this won't work is this - once a relationship is created, Sequelize offers you some magic methods which you can use.让我觉得这行不通的原因是——一旦建立了关系,Sequelize 就会为您提供一些可以使用的神奇方法。 For example in my case I have this:例如,在我的情况下,我有这个:

let name = req.body.name;
let thumbnail = req.body.image;
let userId = req.body.userId

let user = await User.findByPk(userId)

let socketServer = await Server.create({
    name: name,
    thumbnail: thumbnail
});

user.addServer(socketServer)

Since I've defined a relationship between User and Server, I can use addServer() which does one of two things depending on whether the relationship between the models is one-to-many or many-to-many.由于我已经定义了用户和服务器之间的关系,我可以使用 addServer() 根据模型之间的关系是一对多还是多对多来执行两件事之一。

  1. If I've defined many-to-many relationship, executing user.addServer(socketServer) will create a record in the junction table that will link the userId and serverId.如果我定义了多对多关系,则执行user.addServer(socketServer)将在联结表中创建一条记录,该记录将链接 userId 和 serverId。

  2. If I've defined one-to-many relationship, executing user.addServer(socketServer) will update the userId column of the server to the id of user如果我定义了一对多关系,执行user.addServer(socketServer)会将服务器的 userId 列更新为用户的 id

So now that I've applied both relationships between the models, when I execute user.addServer(socketServer) , I only update the userId column of the server with the id of the user BUT I don't get a record in the junction table that creates the many-to-many relationship.所以现在我已经应用了模型之间的两种关系,当我执行user.addServer(socketServer)时,我只用用户的 id 更新服务器的 userId 列但是我没有在联结表中得到记录这创建了多对多的关系。

This leads me to believe I can only use one relationship at a time but how am I to achieve what I'm trying to achieve without the 2 relationships?这让我相信我一次只能使用一种关系,但是如果没有这两种关系,我该如何实现我想要实现的目标呢?

opinion观点

Your logic you addressed seems to me that User creates server and user within the server creates server on and on.在我看来,您提出的逻辑似乎是用户创建服务器,而服务器内的用户不断创建服务器。 Is the consequence correct?结果正确吗?

shouldn't the one user create servers and other users go in to the servers一个用户不应该创建服务器和其他用户 go 到服务器

my suggetion is create one to many among user and server and have privillage table so the one user created the server have the authority to the what ever he or she wants.我的建议是在用户和服务器之间创建一对多并具有特权表,因此创建服务器的一个用户有权获得他或她想要的任何东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM