how do we change column type in migration. In my migration 1 I have a migration that added the column. Now I want to change the column type from string to text , should I create a new migration file which is like changeColumn or I can create new migration file the same with migration 1 but I have just to change the type to text ? Thank you.
#Migration 1
await queryInterface.addColumn(SampleModel.tableName, 'name', {
type: Sequelize.STRING,
allowNull: true,
}, {
transaction,
});
Migration 2 (does creating new migration would work like this ? still addColumn but i change the type to text)
await queryInterface.addColumn(SampleModel.tableName, 'name', {
type: Sequelize.TEXT,
allowNull: true,
}, {
transaction,
});
changeColumnbugs to be aware ofI was really confused by those on 6.5.1, so I thought it would be good to leave warning here.
Bug 1: SQLite triggers on delete cascades
SQLite does not have
ALTER COLUMNas mentioned at:ALTER COLUMN in sqlite
https://sequelize.org/api/v6/class/src/dialects/sqlite/query-interface.js~sqlitequeryinterface
The problem is that sequelize does not take
ON DELETE CASCADEon doing this, so when it gets rid of the original table to recreate it, this triggers the cascades, and so you lose data.What they would need to do is to temporarily drop any cascades before the migration and restore them afterwards. I'm not sure how to work around this except by surrounding a migration with a migration that drops the cascade and another one that restores them.
Thankfully in my use case it doesn't matter that much, as SQLite is just a quick way to develop locally, so it is fine if it nukes the DB every time as I can just reseed it. Using SQLite locally is a bad habit actually considering the inconsistencies across Sequelize support for different DBMSs.
Bug 2: you have to pass
type:on PostgreSQL even if you are not changing itFor example, if you wanted to change just the default value of a column you might want to do:
but on PostgreSQL this gives an error:
For some reason, it does not give a backtrace, I love Node.js and its ecosystem, so after an hour of debugging I found that the
typeis required. Supposing that the column was of typeDataTypes.STRING, we would do:otherwise it tries to do a
type.toString()at: https://github.com/sequelize/sequelize/blob/v6.5.1/lib/dialects/postgres/query-generator.js#L466 buttypeis undefined.