Sequelize.js foreign key

前端 未结 5 1619
长发绾君心
长发绾君心 2020-12-24 05:26

When using Sequelize.js, the following code doesn\'t add any foreign key on tables.

var MainDashboard = sequelize.define(\'main_dashboard\', {
  title: Sequ         


        
5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-24 06:20

    For Sequelize 4 this has been updated to the following:

    const Father = sequelize.define('Father', {
            name: Sequelize.STRING
    });
    
    const Child = sequelize.define('Child', {
        age: Sequelize.STRING,
        fatherId: {
           type: Sequelize.INTEGER,
           references: {
              model: 'fathers', // 'fathers' refers to table name
              key: 'id', // 'id' refers to column name in fathers table
           }
        }
    });
    
    Father.hasMany(Child); // Set one to many relationship
    

    Edit: You can read more on associations at https://sequelize.org/master/manual/assocs.html

提交回复
热议问题