Alembic SQLite ALTER TABLE with self-referencing foreign key Alembic SQLite ALTER TABLE with self-referencing foreign key sqlite sqlite

Alembic SQLite ALTER TABLE with self-referencing foreign key


After some research I found that the problem here is the way Alembic does the batch migration. In short, at the current version (0.7.6) of Alembic it's not possible to create relation with self by migration.

  1. As described in the Alembic documentation, to do the migration, new table is created with a temporary name and changes from the alter tablecode. In this case:

    CREATE TABLE _alembic_batch_temp (    id VARCHAR(24) NOT NULL,     parent_id VARCHAR(24),     PRIMARY KEY (id),     CONSTRAINT parent_constraint FOREIGN KEY(parent_id) REFERENCES _alembic_batch_temp (id))
  2. The table is filled with the data from the old table:

    INSERT INTO _alembic_batch_temp (id) SELECT id FROM my_table;
  3. Then the old table is removed:

    DROP TABLE my_table;
  4. Finally the newly created table is renamed to it's proper name:

    ALTER TABLE _alembic_batch_temp RENAME TO my_table;

The problem with this way of doing things is already visible in the first code snippet. The newly created foreign key is referencing the temporary table and once it's created it can't be changed due to restrictions in SQLite. So after the renaming of the table you end up with the table you provided:

CREATE TABLE "my_table" (  # new name    id VARCHAR(24) NOT NULL,     parent_id VARCHAR(24),     PRIMARY KEY (id),     CONSTRAINT parent_constraint FOREIGN KEY(parent_id) REFERENCES _alembic_batch_temp (id)  # old reference)

To Avoid this situation you can create the batch migration manually:

  1. Rename the old table to some temporary name:

    ALTER TABLE my_table RENAME TO migration_temp_table;
  2. Create new table with proper name and proper reference:

    CREATE TABLE my_table (    id VARCHAR(24) NOT NULL,     parent_id VARCHAR(24),     PRIMARY KEY (id),     CONSTRAINT parent_constraint FOREIGN KEY(parent_id) REFERENCES my_table (id))
  3. Copy the data:

    INSERT INTO my_table (id) SELECT id FROM migration_temp_table;
  4. Remove the old table:

    DROP TABLE migration_temp_table;