简体   繁体   English

Sequelize-在模型中使用“设置”,“添加”,“创建”方法以及“属于”关联

[英]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. 我是使用sequelize的新手,但我还不知道在将模型与BelongsToMany关联相关联时如何实现sequelize创建的方法。 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. 我有两个模型“用户”和“属性”,一个用户可以具有多个属性,由于每种原因,当我有userId,propertyId,status和其他字段时,我都会创建中间模型PropertiesByUser。

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? 我要分别创建用户和属性,但是要为用户添加属性的过程对我来说很复杂,因为我不知道如何创建或使用模型PropertiesByUser进行创建,添加,设置,属性用户,或将用户添加到媒体资源,有人可以帮助我吗?

I've read in blogs, some ways to use this but, any solution works. 我已经在博客中阅读了一些使用它的方法,但是任何解决方案都可以。 :( :(

I suggest you start with Belongs to Many Doc . 我建议您从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). 在这里,您可以看到可以使用的功能(只需将Association替换为您的实际名称)。

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! 祝好运!

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

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