-
-
Notifications
You must be signed in to change notification settings - Fork 301
Description
Migrated issue, originally created by Charles Leifer (@coleifer)
Hi, thanks for your excellent work on this project (and SQA).
I'm using Flask-migrate and ran into a small issue adding a column that had a foreign key constraint. The column was added, but the constraint was not created. I wasn't sure if this was operator error on my part, but thought I'd share it anyway:
I made some model changes in my project and noticed that a foreign key constraint wasn't getting picked up when I ran migrate. Basically I had a blog Entry model and added a User model to represent the author. I also added a foreign key from the entry to the user.
Here are portions of the models:
class Entry(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100))
author_id = db.Column(db.Integer, db.ForeignKey('user.id')) # New column
class User(db.Model): # New model
id = db.Column(db.Integer, primary_key=True)This is the output of db migrate:
INFO [alembic.migration] Context impl SQLiteImpl.
INFO [alembic.migration] Will assume non-transactional DDL.
INFO [alembic.autogenerate.compare] Detected added table 'user'
INFO [alembic.autogenerate.compare] Detected added column 'entry.author_id'
Here is the migration:
### commands auto generated by Alembic - please adjust! ###
op.create_table('user',
sa.Column('id', sa.Integer(), nullable=False),
....
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('email'),
sa.UniqueConstraint('slug')
)
op.add_column(u'entry', sa.Column('author_id', sa.Integer(), nullable=True))
### end Alembic commands ###Notice that the foreign key constraint is missing.
I upgraded the db and, while the author_id column is there, the foreign key constraint is not:
CREATE TABLE entry (
id INTEGER NOT NULL,
title VARCHAR(100),
author_id INTEGER,
PRIMARY KEY (id),
UNIQUE (slug)
);Versions:
- Flask-Migrate==1.2.0
- Flask-SQLAlchemy==1.0
- SQLAlchemy==0.9.1
- alembic==0.6.2