Creating instance with an association in Sequelize

后端 未结 8 1018
清酒与你
清酒与你 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

    0 讨论(0)
  • 2021-02-02 09:28

    Assuming you have the right association between users and login, you can just create a user including a login:

    User.create({
       name: "name",
       Login: {...}
    },{
       include: Login
    })
    

    you can find more information here: http://docs.sequelizejs.com/manual/tutorial/associations.html#creating-with-associations

    0 讨论(0)
  • 2021-02-02 09:28

    I have the same issue recently! I have a typo mistake with the foreignKey config. Use field instead of name caused the issue.

    The change below will fix it.

    {
        foreignKey: {
           name: 'userId',
           allowNull: false,
        }
    }
    
    0 讨论(0)
  • 2021-02-02 09:32

    As an extra you could also nest your creation to be even more effective and concise.

      // Set up the models
        var User = sequelize.define('User', {});
        var Login = sequelize.define('Login', {});
            ...
    
    
        User.create({
           name: "name",
           Login:
                {
                users: {..i.e several users if a user belongs to another user..}
                }
            },{
           include:{
          model: Login,
          include: User //nested model.Create
             }
        })
    

    as seen here: https://github.com/sequelize/sequelize/issues/7252

    0 讨论(0)
  • 2021-02-02 09:34

    First of all you need to setup the relations in both ways, like this:

    // Set up the models
    var User = sequelize.define('User', {});
    var Login = sequelize.define('Login', {});
    
    // Set the correct associations
    User.hasMany(Login, {})
    Login.belongsTo(User, {});
    

    Then, you need to properly get the instances returned by the promises:

    // Create the instances
    User.create({}).then(function(newUser) {
        // now you can use newUser acessors to create the login
        return newUser.createLogin({});
    ).then(function(newLogin){
        // newLogin
    }).catch(function(error){
        // error
    });
    
    0 讨论(0)
  • 2021-02-02 09:38

    You have association between User an Login with constraint allowNull at false. You must create Login before User or set allowNull at true in model and the table to DB (LoginId Null constraint)

    var User = sequelize.define('User', {});
    var Login = sequelize.define('Login', {});
    Login.belongsTo(User, {
      onDelete: 'cascade',
      foreignKey: {
        field: 'userId',
        allowNull: false,
      }
    });
    

    Solution

    Login.create({
       username: "username",
       User: {...}
    },{
       include: User
    })
    
    0 讨论(0)
提交回复
热议问题