changeColumn
bugs to be aware of
I 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 COLUMN
as mentioned at:
The problem is that sequelize does not take ON DELETE CASCADE
on 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 it
For example, if you wanted to change just the default value of a column you might want to do:
up: async (queryInterface, Sequelize) => queryInterface.sequelize.transaction(async transaction => {
await queryInterface.changeColumn('User', 'ip',
{
defaultValue: undefined,
}, { transaction }
);
})
but on PostgreSQL this gives an error:
ERROR: Cannot read property 'toString' of undefined
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 type
is required. Supposing that the column was of type DataTypes.STRING
, we would do:
await queryInterface.changeColumn('User', 'ip',
{
type: Sequelize.DataTypes.STRING,
defaultValue: undefined,
}, { transaction }
);
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 but type
is undefined.