Sequelize模型关联-外键缺失

时间:2019-05-02 18:21:37

标签: postgresql foreign-keys sequelize.js associations sequelize-cli

我有2个这样关联的模型。客户通过1:M关系与应用程序关联。

客户:

'use strict';
module.exports = (sequelize, DataTypes) => {

let customer =  sequelize.define('customer', {
  id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      primaryKey: true,
      autoIncrement: true
  },
  name: {
      type: DataTypes.STRING
  },
  account_id: {
      type: DataTypes.STRING
  },
  code: {
      type: DataTypes.STRING
  },
  createdAt: {
      type: DataTypes.DATE,
      defaultValue: sequelize.literal('NOW()')
  },
  updatedAt: {
      type: DataTypes.DATE,
      defaultValue: sequelize.literal('NOW()')
  }
},
{
  underscored: true,
  freezeTableName: true,
  tableName: 'customer'
});
customer.associate = function(models) {
    // associations can be defined here
    customer.hasMany(models.application, { foreignKey: 
    'customer_id' });

    };

sequelize.sync()
    .then(() => customer.create(
        { name: "customer1", account_id: "cust-1-acct-1", code: "ACME Inc." }
        )).then(function(customer) {
        console.log('customers created');
    }).then(() => customer.create(
    { name: "customer2", account_id: "cust-2-acct-2", code: "test Cust" }
)).then(function(customer) {
    console.log('customers created');
})
.catch(function(err) {
    console.log(err);
});

return customer;
}

应用程序:

'use strict';

module.exports = (sequelize, DataTypes) => {

let application = sequelize.define('application', {
        id: {
            type: DataTypes.INTEGER,
            allowNull: false,
            primaryKey: true,
            autoIncrement: true
        },
        name: {
            type: DataTypes.STRING,
            sortable: true
        },
        creation_date: {
            type: DataTypes.NUMERIC,
            sortable: true
        },
        customer_id: {
            type: DataTypes.INTEGER
        },
        createdAt: {
            type: DataTypes.DATE,
            defaultValue: sequelize.literal('NOW()')
        },
        updatedAt: {
            type: DataTypes.DATE,
            defaultValue: sequelize.literal('NOW()')
        }
    },
    {
        underscored: true,
        freezeTableName: true,
        tableName: 'application'
    });
application.associate = function(models) {
    // associations can be defined here
    application.belongsTo(models.customerView, { through: 'customer_id' });

};

sequelize.sync()
    .then(() => application.create(
        { customer_id: "1", name: "application 1", creation_date: "1556724178700" }
    )).then(() => application.create(
        { customer_id: "1", name: "application 2", creation_date: "1556724178700" }
    )).then(() => application.create(
        { customer_id: "2", name: "application 3", creation_date: "1556724178700" }
    ))
    .then(function(application) {
    console.log('applications created');
})
.catch(function(err) {
    console.log(err);
});

return application;
}

将按预期创建这2个表,但没有我期望的外键约束。外键应位于应用程序表的customer_id上。

我在做什么错?

0 个答案:

没有答案