You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Sqlite remove/change/rename column don't pass foreign key references along in the new table #7078
I'm trying to rename a column on an Sqlite db. I use renameColumn on the QueryInterface that, in the Sqlite case, is going to call renameColumn on the SQLiteQueryInterface.
What do you expect to happen?
I expect that:
a temporary table is created equal to the first one (except the renamed column) including foreign key references.
data is copied over to the temporary table
old table is deleted
a new table is created equal to the first one (except the renamed column) including foreign key references.
data is copied over to the new table
What is actually happening?
a temporary table is created equal to the first one (except the renamed column) NOT including foreign key references.
data is copied over to the temporary table
old table is deleted
a new table is created equal to the first one (except the renamed column) NOT including foreign key references.
data is copied over to the new table
This is the original table:
CREATETABLE `matches` (`id`INTEGERPRIMARY KEY AUTOINCREMENT, `hash`VARCHAR(200) NOT NULL UNIQUE, `alertId`INTEGERREFERENCES`alerts` (`id`) ON DELETE CASCADEONUPDATE CASCADE)
This is the table after the renameColumn on 'hash' -> 'foo'
CREATETABLE `matches` (`id`INTEGERPRIMARY KEY AUTOINCREMENT, `foo`VARCHAR(200) NOT NULL, `alertId`INTEGER)
A solution, not sure if correct, would be to create a particular case of QueryInterface::describeTable for Sqlite, implementing SQLiteQueryInterface::describeTable s.t. it doesn't use
PRAGMA TABLE_INFO('tableName');`
but
SELECT type, sql FROM sqlite_master WHERE tbl_name='tableName'
By doing so, we know which one are the foreign key references of the original table, a thing that we don't get with the pragma query
The correct result of the new describeTable would be used by renameColumnQuery and removeColumnQuery
#7108 didn't address this issue unfortunately..in my case, I'm using a removeColumn in a migration (sqlite dialect). The temporary table loses a UNIQUE constraint. I'll try to get an example project together. Until then, going to try and get back 3 hours of my life. :)
I am avoiding similar problem in the following bad patch code.
This patch modify queryInterface.describeTable method.
functionup(queryInterface){letbackupDescribeTable=queryInterface.describeTable;queryInterface.describeTable=asyncfunction(tableName,options){lettableDatas=awaitbackupDescribeTable.call(queryInterface,tableName,options);// add auto increment to id columntableDatas.id.autoIncrement=true;// get foreign key infoletforeignKeyDatas=(awaitqueryInterface.sequelize.query(`PRAGMA FOREIGN_KEY_LIST("${tableName}");`))[0];for(letforeignKeyDataofforeignKeyDatas){lettableData=tableDatas[foreignKeyData.from];tableData.references={model: foreignKeyData.table,key: foreignKeyData.to};tableData.onDelete=foreignKeyData.on_delete;tableData.onUpdate=foreignKeyData.on_update;}returntableDatas;};changeorremoveorrenameColumn(...);}
This patch can restoration of foreign keys.
AutoIncrement is force set to id column.
functionup(queryInterface){letbackupDescribeTable=queryInterface.describeTable;queryInterface.describeTable=asyncfunction(tableName,options){lettableDatas=awaitbackupDescribeTable.call(queryInterface,tableName,options);// add auto increment to id columntableDatas.id.autoIncrement=true;// get foreign key infoletforeignKeyDatas=(awaitqueryInterface.sequelize.query(`PRAGMA FOREIGN_KEY_LIST("${tableName}");`))[0];for(letforeignKeyDataofforeignKeyDatas){lettableData=tableDatas[foreignKeyData.from];tableData.references={model: foreignKeyData.table,key: foreignKeyData.to};tableData.onDelete=foreignKeyData.on_delete;tableData.onUpdate=foreignKeyData.on_update;}returntableDatas;};changeorremoveorrenameColumn(...);}
I just did a migration with renameColumn() and lost all the foreign keys in other tables to that referenced table I did the column rename on.
Sequelize: v6.5.0
Sequelize-CLI: v6.2.0
SQLite3: v5.0.2
Activity
francescoinfante commentedon Jan 6, 2017
Even UNIQUE are lost.
francescoinfante commentedon Jan 6, 2017
A solution, not sure if correct, would be to create a particular case of
QueryInterface::describeTablefor Sqlite, implementingSQLiteQueryInterface::describeTables.t. it doesn't usebut
By doing so, we know which one are the foreign key references of the original table, a thing that we don't get with the pragma query
The correct result of the new describeTable would be used by
renameColumnQueryandremoveColumnQuerysushantdhiman commentedon Jan 10, 2017
Some old related issue, this issue might be affecting other dialects.
#2033
#6205
#3466
#5007
francescoinfante commentedon Jan 10, 2017
Any idea other than using:
This query returns the original CREATE TABLE statement. Parsing it is probably not an option.
bartcone commentedon Dec 19, 2017
#7108 didn't address this issue unfortunately..in my case, I'm using a
removeColumnin a migration (sqlite dialect). The temporary table loses a UNIQUE constraint. I'll try to get an example project together. Until then, going to try and get back 3 hours of my life. :)sushantdhiman commentedon Dec 20, 2017
No stale bot closed it, its meant to be kept open
yikescojp commentedon Feb 1, 2018
I am avoiding similar problem in the following bad patch code.
This patch modify queryInterface.describeTable method.
This patch can restoration of foreign keys.
AutoIncrement is force set to id column.
11 remaining items
dyc3 commentedon Jun 24, 2020
I've created a rough fix that fixes the behavior for the sscce here: sequelize/sequelize-sscce#86
Is there an sscce for the foreign key thing?
Is this code still relevant?
fix(sqlite): describeTable now returns unique constraint, fixes seque…
fix(sqlite): describeTable now returns unique constraint, fixes seque…
fix(sqlite): describeTable now returns unique constraint, fixes seque…
fix(sqlite): describeTable now returns unique constraint, fixes seque…
fix(sqlite): describeTable now returns unique constraint, fixes seque…
fix(sqlite): describeTable now returns references, fixes sequelize#7078
fix(sqlite): describeTable now returns unique constraint, fixes seque…
fix(sqlite): describeTable now returns references, fixes sequelize#7078
sushantdhiman commentedon Jun 28, 2020
🎉 This issue has been resolved in version 6.2.3 🎉
The release is available on:
Your semantic-release bot 📦🚀
sushantdhiman commentedon Jun 30, 2020
🎉 This issue has been resolved in version 5.22.3 🎉
The release is available on:
Your semantic-release bot 📦🚀
Androphin commentedon Feb 28, 2021
I just did a migration with renameColumn() and lost all the foreign keys in other tables to that referenced table I did the column rename on.
Sequelize: v6.5.0
Sequelize-CLI: v6.2.0
SQLite3: v5.0.2
Uko commentedon Apr 7, 2021
changeColumn() removes the onUpdate and onDelete constarints from the foeigh keys…
Sequelize: v6.6.2
Sequelize-CLI: v6.2.0
SQLite3: v5.0.0