cli icon indicating copy to clipboard operation
cli copied to clipboard

allowNull false is not set on changeColumn

Open virtser opened this issue 8 years ago • 14 comments

I have two migrations scripts.

The first one adds a new column to table and with allowNull: true:

    return queryInterface.addColumn(
      'likes',
      'apartment_id',
      {
        type: Sequelize.INTEGER,
        allowNull: true
      }
    );

The following one sets allowNull: false and adds a reference to another table as FK:

    return queryInterface.changeColumn(
      'likes',
      'apartment_id',
      {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'apartments',
          key: 'id'
        }
      }
    );

Eventually after all migrations were executed, the filed in MySQL DB still allows NULL:

`apartment_id` int(11) DEFAULT NULL,

virtser avatar Jun 20 '17 09:06 virtser

Looking at the code, there doesn't seem to be a reason why that would fail. Can you show the output SQL that was generated?

Americas avatar Jun 23 '17 13:06 Americas

same issue. can you update if solution is found

ArslanAnjum avatar Mar 20 '18 11:03 ArslanAnjum

It doesn't work for me too, How do i get SQL output for the migration?

hamxabaig avatar Aug 07 '18 11:08 hamxabaig

For some reason, allowNull: false on a field with reference doesn't work on my end and throws SequelizeDatabaseError: Cannot add foreign key constrain . Info: Node 8.10.0, sequelize 4.38.0 with sequelize-cli 4.0.0.

suavedev avatar Aug 31 '18 19:08 suavedev

Hi everyone. Was the issue fixed?

nlevchuk avatar Jan 05 '19 10:01 nlevchuk

Having the same problem here but adding unique: true instead of allowNull: false. Any solution?

LeoDoldan7 avatar Mar 12 '19 19:03 LeoDoldan7

same issue with node 10.11.0 and sequelize 4.42.0, both with addColumn() and changeColumn()

ghost avatar Mar 27 '19 19:03 ghost

I think you have to use queryInterface.removeConstraint in order to make it work.

LeoDoldan7 avatar May 24 '19 18:05 LeoDoldan7

You can't have any rows with a null value in if you're changing that column to allowNull

karltaylor avatar Jun 18 '19 20:06 karltaylor

You can't have any rows with a null value in if you're changing that column to allowNull

If there is no records/rows in the table, you should be able to create a column with allowNull: false... yet it doesn't seem to be possible with createColumn or changeColumn.

But there is a solution to this one. You must add the foreign key constraint when you create the table. That means that you must first create the referenced table.

If you must change a column in an existing table with existing records, then I don't know.

kasp4770 avatar Jan 27 '20 09:01 kasp4770

Not exactly the same issue, but I also faced a problem trying to update a column allowing null values (from not null constraint)

In my case I had to define a default value (defaultValue), otherwise the migration file was executed successfully but the null constraint was still there.

module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.changeColumn(table, column, {
      type: Sequelize.BIGINT,
      allowNull: true,
      defaultValue: null
    })
  },

  down: (queryInterface, Sequelize) => {
    return queryInterface.changeColumn(table, column, {
      type: Sequelize.BIGINT,
      allowNull: false,
    })
  }
}

adrigardi90 avatar Mar 03 '20 15:03 adrigardi90

Dear, it's not work for me

      isNight: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: false,
      },

jeff-le-goldenowl avatar Apr 23 '20 06:04 jeff-le-goldenowl

Not exactly the same issue, but I also faced a problem trying to update a column allowing null values (from not null constraint)

In my case I had to define a default value (defaultValue), otherwise the migration file was executed successfully but the null constraint was still there.

module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.changeColumn(table, column, {
      type: Sequelize.BIGINT,
      allowNull: true,
      defaultValue: null
    })
  },

  down: (queryInterface, Sequelize) => {
    return queryInterface.changeColumn(table, column, {
      type: Sequelize.BIGINT,
      allowNull: false,
    })
  }
}

Yes also had to define the type to make it work.

ogtfaber avatar Jun 03 '20 00:06 ogtfaber

Same with references :disappointed: in sequelize 6.28 with Node v18.

01.migrate

// Setup
await createTable('tn',{col: {reference:{...},allowNull:true});

02.migrate

// does not set NULL
await changeColumn('tn','col', {allowNull:true,type: DataTypes.BIGINT.UNSIGNED,references:{...}})
// sets NULL
await changeColumn('tn','col', {allowNull:true,type: DataTypes.BIGINT.UNSIGNED})

flipflopsimsommer avatar Mar 09 '23 12:03 flipflopsimsommer