Sequelize.js foreign key

前端 未结 5 1586
长发绾君心
长发绾君心 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:18

    It's amazingly simple.

    const MainDashboard = this.sequelize.define('main_dashboard', {/* attributes */})
      , MainClient = this.sequelize.define('main_client', {/* attributes */});
    
    MainDashboard.belongsTo(MainClient, { foreignKey: 'clientId' }); // Adds clientId to MainDashboard
    

    It will link this as a foreign key and you may use it as an association. Let me know if I'm missing anything.

提交回复
热议问题