How to save the owner of a group in Sequelize
P粉378264633
P粉378264633 2023-09-13 14:14:39
0
1
580

I'm working with Sequelize and Koa. I created a user model and a group model using Sequelize-CLI. There is a many-to-many relationship between them. I want to store which user is the owner of the group by using his UUID. Do I create another association using a one-to-many association (1 user is the owner of 0 or more groups). I don't know how to create this association on top of many to many. Or do I just store the user's UUID in my group model? What's the best way to solve this problem? Thanks!

I searched online for examples of similar situations but found none.

P粉378264633
P粉378264633

reply all(1)
P粉410239819

If a group can have a unique owner, then obviously you need to add something like ownerId to the Group and add an association like this: p>

User.hasMany(Group, { as: 'OwnedGroups', foreignKey: 'ownerId' })
Group.belongsTo(User, { as: 'Owner', foreignKey: 'ownerId' }
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template