Skip to content

Sqlite remove/change/rename column don't pass foreign key references along in the new table #7078

Closed

Description

@francescoinfante

What you are doing?

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:

  1. a temporary table is created equal to the first one (except the renamed column) including foreign key references.
  2. data is copied over to the temporary table
  3. old table is deleted
  4. a new table is created equal to the first one (except the renamed column) including foreign key references.
  5. data is copied over to the new table

What is actually happening?

  1. a temporary table is created equal to the first one (except the renamed column) NOT including foreign key references.
  2. data is copied over to the temporary table
  3. old table is deleted
  4. a new table is created equal to the first one (except the renamed column) NOT including foreign key references.
  5. data is copied over to the new table

This is the original table:

CREATE TABLE `matches` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `hash` VARCHAR(200) NOT NULL UNIQUE, `alertId` INTEGER REFERENCES `alerts` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)

This is the table after the renameColumn on 'hash' -> 'foo'

CREATE TABLE `matches` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `foo` VARCHAR(200) NOT NULL, `alertId` INTEGER)

Dialect: sqlite
Database version: 3.9.x
Sequelize version: 3.24.x

Activity

  1. francescoinfante commented on Jan 6, 2017

    @francescoinfante
    ContributorAuthor

    Even UNIQUE are lost.

  2. francescoinfante commented on Jan 6, 2017

    @francescoinfante
    ContributorAuthor

    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

  3. sushantdhiman commented on Jan 10, 2017

    @sushantdhiman
    Contributor

    Some old related issue, this issue might be affecting other dialects.

    #2033
    #6205
    #3466
    #5007

  4. added
    type: bug
    type: refactor
    on Jan 10, 2017
  5. francescoinfante commented on Jan 10, 2017

    @francescoinfante
    ContributorAuthor

    Any idea other than using:

    SELECT type, sql FROM sqlite_master WHERE tbl_name='tableName'

    This query returns the original CREATE TABLE statement. Parsing it is probably not an option.

  6. bartcone commented on Dec 19, 2017

    @bartcone

    #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. :)

  7. sushantdhiman commented on Dec 20, 2017

    @sushantdhiman
    Contributor

    No stale bot closed it, its meant to be kept open

  8. yikescojp commented on Feb 1, 2018

    @yikescojp

    I am avoiding similar problem in the following bad patch code.
    This patch modify queryInterface.describeTable method.

    function up(queryInterface) {
      let backupDescribeTable = queryInterface.describeTable;
      queryInterface.describeTable = async function (tableName, options) {
        let tableDatas = await backupDescribeTable.call(queryInterface, tableName, options);
        // add auto increment to id column
        tableDatas.id.autoIncrement = true;
        // get foreign key info
        let foreignKeyDatas = (await queryInterface.sequelize.query(`PRAGMA FOREIGN_KEY_LIST("${tableName}");`))[0];
        for (let foreignKeyData of foreignKeyDatas) {
          let tableData = tableDatas[foreignKeyData.from];
          tableData.references = {model: foreignKeyData.table, key: foreignKeyData.to};
          tableData.onDelete = foreignKeyData.on_delete;
          tableData.onUpdate = foreignKeyData.on_update;
        }
        return tableDatas;
      };
      change or remove or renameColumn(...);
    }

    This patch can restoration of foreign keys.
    AutoIncrement is force set to id column.

  9. 11 remaining items

  10. dyc3 commented on Jun 24, 2020

    @dyc3
    Contributor

    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?

    function up(queryInterface) {
      let backupDescribeTable = queryInterface.describeTable;
      queryInterface.describeTable = async function (tableName, options) {
        let tableDatas = await backupDescribeTable.call(queryInterface, tableName, options);
        // add auto increment to id column
        tableDatas.id.autoIncrement = true;
        // get foreign key info
        let foreignKeyDatas = (await queryInterface.sequelize.query(`PRAGMA FOREIGN_KEY_LIST("${tableName}");`))[0];
        for (let foreignKeyData of foreignKeyDatas) {
          let tableData = tableDatas[foreignKeyData.from];
          tableData.references = {model: foreignKeyData.table, key: foreignKeyData.to};
          tableData.onDelete = foreignKeyData.on_delete;
          tableData.onUpdate = foreignKeyData.on_update;
        }
        return tableDatas;
      };
      change or remove or renameColumn(...);
    }

    Is this code still relevant?

  11. added 8 commits that reference this issue on Jun 24, 2020
    be3dff4
    250c74e
    ed46562
    5bceb3f
    3be8877
    ae8cef1
    dbfda0d
    b5f8711
  12. sushantdhiman commented on Jun 28, 2020

    @sushantdhiman
    Contributor

    🎉 This issue has been resolved in version 6.2.3 🎉

    The release is available on:

    Your semantic-release bot 📦🚀

  13. sushantdhiman commented on Jun 30, 2020

    @sushantdhiman
    Contributor

    🎉 This issue has been resolved in version 5.22.3 🎉

    The release is available on:

    Your semantic-release bot 📦🚀

  14. Androphin commented on Feb 28, 2021

    @Androphin

    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

  15. Uko commented on Apr 7, 2021

    @Uko

    changeColumn() removes the onUpdate and onDelete constarints from the foeigh keys…

    Sequelize: v6.6.2
    Sequelize-CLI: v6.2.0
    SQLite3: v5.0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

releasedtype: bugtype: refactor

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions