0

I'm trying to write a migration in Sequelize and want to remove the defaultValue constraint. What is the proper syntax? I've tried both of the following:

return queryInterface.removeConstraint('Table', 'table_columnName_default')
return queryInterface.removeConstraint('Table', 'columnName_default')
clewiston
  • 53
  • 1
  • 5

3 Answers3

7

Can you please try to use

return queryInterface.changeColumn('Table', 'attributeName', {
    defaultValue: null,
    allowNull: true,
});

http://docs.sequelizejs.com/class/lib/query-interface.js~QueryInterface.html#instance-method-changeColumn

Rahul Sharma
  • 1,393
  • 10
  • 19
0

You can also use a raw query:

return queryInterface.sequelize.query(`ALTER TABLE table ALTER COLUMN column DROP DEFAULT;`)
pwerth
  • 210
  • 1
  • 6
  • 14
0

you should know the data type. for e.g. STRING:

const { DataTypes } = require('sequelize');

return queryInterface.changeColumn('Table', 'attributeName', {
    defaultValue: null,
    type: DataTypes.STRING
});

you can also use queryInterface from the model definition (or pass the model/query interface) to make it generic for any column with default value:

function dropDefaults(queryInterface, model) {
  const defaultValues = Object.keys(model._defaultValues);
  for (const column of defaultValues) {
    model.queryInterface.changeColumn(model.tableName, column, {
      defaultValue: null,
      type: model.fieldRawAttributesMap[column].type,
    });
  }
}
Nben
  • 127
  • 7