1

I have 2 models that I am associating like this. Customer is associated to application by 1:M relationship.

customer:

'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;
}

application:

'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;
}

These 2 tables are getting created as expected, but without the foreign key constraint that I am expecting. The foreign key should be on the application table, on customer_id.

What am I doing wrong?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.