Creating instance with an association in Sequelize

后端 未结 8 1128
清酒与你
清酒与你 2021-02-02 09:09

Using Sequelize, I\'ve created two models: User and Login.

Users can have more than one Login, but a login must have exactly one user, which me

8条回答
  •  误落风尘
    2021-02-02 09:23

    An Update to @Arwed Mett's answer

    //Create Association Alias or just setting association alias by using 'as' keyword will also work
    Login.User = Login.belongsTo(User);
    
    User.create({
     name: "name",
      Login: {...}
    }, {
      include: [{
        association: Login.User
      }]
    });
    

    Refrence link - http://docs.sequelizejs.com/manual/tutorial/associations.html#creating-with-associations

提交回复
热议问题