Alembic transition for SQLite database:
def upgrade(): with op.batch_alter_table('my_table') as batch_op: batch_op.add_column(sa.Column('parent_id', sa.String(24))) batch_op.create_foreign_key('parent_constraint', 'my_table', ['parent_id'], ['id'])
which should create a foreign key parent_id , referencing the id the same table my_table , creates a link to a table named _alembic_batch_temp :
CREATE TABLE "my_table" ( id VARCHAR(24) NOT NULL, parent_id VARCHAR(24), PRIMARY KEY (id), CONSTRAINT parent_constraint FOREIGN KEY(parent_id) REFERENCES _alembic_batch_temp (id) )
How to create restrictions for self-regulation when changing a table?
source share