Repository: sequelize/sequelize

Issue #6586: onDelete and sync() depends on order of associations
Created: 2016-09-10T20:50:56Z
This seems like it may be related to some issues already reported, but I couldn't quite find it. Apologies if it's a duplicate.
What you are doing?
Here are two minimal models, and two orders of adding associations between them.
const Sequelize = require("sequelize");
const sequelize = new Sequelize(/*...*/);

let a = sequelize.define("a", {
  id: { type: Sequelize.INTEGER, allowNull: false, primaryKey: true, autoIncrement: true },
}, {freezeTableName: true, timestamps: false});
let b = sequelize.define("b", {
  id: { type: Sequelize.INTEGER, allowNull: false, primaryKey: true, autoIncrement: true },
  aId: { type: Sequelize.INTEGER, allowNull: false }
}, {freezeTableName: true, timestamps: false});

if (process.argv[2] == "1") {
  a.hasMany(b);
  b.belongsTo(a);
} else {
  b.belongsTo(a);
  a.hasMany(b);
}
sequelize.sync({force: true, logging: console.log}).then(() => process.exit(0));```
What do you expect to happen?
The created foreign key probably shouldn't depend on whether we're in case 1 or 2.
What is actually happening?
$ node test.js 1 | grep CREATE
Executing (default): CREATE TABLE IF NOT EXISTS "a" ("id"   SERIAL , PRIMARY KEY ("id"));
Executing (default): CREATE TABLE IF NOT EXISTS "b" ("id"   SERIAL , "aId" INTEGER NOT NULL 
REFERENCES "a" ("id") ON DELETE CASCADE ON UPDATE CASCADE, PRIMARY KEY ("id"));

$ node test.js 2 | grep CREATE
Executing (default): CREATE TABLE IF NOT EXISTS "a" ("id"   SERIAL , PRIMARY KEY ("id"));
Executing (default): CREATE TABLE IF NOT EXISTS "b" ("id"   SERIAL , "aId" INTEGER NOT NULL 
REFERENCES "a" ("id") ON DELETE NO ACTION ON UPDATE CASCADE, PRIMARY KEY ("id"));
Note that the foreign key has ON DELETE CASCADE in case 1, but ON DELETE NO ACTION in case 2. I think the problem is that hasMany (and hasOne?) default to onDelete: "CASCADE", while belongsTo (and belongsToMany?) default to onDelete: "NO ACTION".
Dialect: postgres
Database version: 9.3.14
Sequelize version: 3.24.2

Duplicate issue #9013: Attribute 'onDelete' override issue
Created: 2018-02-07T01:11:37Z
What are you doing?

I defined both hasMany and belongsTo relationship between User and Quiz model and tried to employ 'CASCADE' strategy for DELETE operation on Quiz model.
//User Model
 const Model = sequelize.define('User', {
    username: {
      type: Sequelize.STRING,
      unique: true,
      allowNull: false
    },
  /* other attributes */
    {
    freezeTableName: true
  });

  Model.associate = (models) => {
    models.User.hasMany(models.Quiz, {
      foreignKey: 'author',
      as: 'quizzes'
    });
  };
//Quiz Model
 const Model = sequelize.define('Quiz', {
    title: {
      type: Sequelize.TEXT,
      allowNull: false
    },
    /* other attributes */
   {
    freezeTableName: true
   }
 );

    models.Quiz.belongsTo(models.User, {
      onDelete: 'CASCADE',
      foreignKey: {
        name: 'author',
        allowNull: false
      }
    });
  };
What do you expect to happen?
I wanted the Quiz Association used CASCADE on delete.
What is actually happening?
The Quiz still used SET NULL  on delete.
Dialect: mysql
Dialect version: 2.15.0
__Database version:__5.7.20
Sequelize version: 4.27
Tested with latest release: No (If yes, specify that version)
Note : Your issue may be ignored OR closed by maintainers if it's not tested against latest version OR does not follow issue template.

Closing comment (2018-02-09T05:44:03Z): Duplicate of #6586