简体   繁体   中英

Sequelize - Use set, add, create methods in models with belongs To Many associations

I'm new working with sequelize, and I don't know yet, how implements the methods that sequelize create when associate models with BelongsToMany associations. For example:

I have two models Users and Properties, a user can have more than one property, for each reason I've create the middle model PropertiesByUser when I have userId, propertyId, status and others fields.

I'm creating the users and properties separately, but when I want add a property to a user is when the process is complicated for me, because I don't know How create or use the model PropertiesByUser for create, add, set, properties for user, or add users to properties, someone can help me please?

I've read in blogs, some ways to use this but, any solution works. :(

I suggest you start with Belongs to Many Doc .

There you can see which functions you'll have access to (just replace the word Association with the actual name of yours).

Also, if you wanna create a property and associate it to an already created user, you can do something like this:

user.createProperty(propertyInfo).then(function() {
   console.log('Done')
})

Note: 'user' is the instance of the user you created, not the name of the model!

Hope this sets you on the right track. Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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