sequelize association explanation on docs

前端 未结 1 1891
不知归路
不知归路 2021-01-29 04:23

while reading sequelize doc association part, explanation below really confuses me

hasOne and belongsTo insert the association key in different models f

1条回答
  •  迷失自我
    2021-01-29 04:45

    Actually it depends on how are you creating your tables. Via sync or via migrations.

    For example

    const User = sequelize.define('user', {
      username: Sequelize.STRING,
    });
    
    const Address = sequelize.define('add', {
      address: Sequelize.STRING,
    });
    
    User.hasOne(Address);
    
    sequelize.sync({ force: true })
      .then(() => User.create({
        username: 'test'
      }))
      .then(item => {
        console.log(item.dataValues.itemPrice);
      });
    

    This will create a userId field in the database

    Note that I am use sync. However if you are using migrations, you would have to create this on your own.

    PS: Its better to use migrations instead of sync.

    0 讨论(0)
提交回复
热议问题