I have a changeset script in which I'm trying to add a foreign key constraint to an existing table column. It looks something like:
from sqlalchemy import *
from migrate import *
import datetime
metadata = MetaData()
registrations = Table('registrations', metadata,
Column('id', Integer(), primary_key=True),
Column('date', DateTime(), nullable=False, index=True,
default=datetime.datetime.now),
Column('device_id', String(255), nullable=False, index=True)
)
devices = Table('devices', metadata,
Column('id', String(255), primary_key=True),
Column('user_id', Integer(), ForeignKey('users.id'), nullable=False,
index=True),
Column('created', DateTime(), nullable=False, index=True,
default=datetime.datetime.now)
)
cons = ForeignKeyConstraint([registrations.c.device_id], [devices.c.id])
def upgrade(migrate_engine):
metadata.bind = migrate_engine
cons.create()
def downgrade(migrate_engine):
metadata.bind = migrate_engine
cons.drop()
When I test the change, SQLAlchemy complains with:
sqlalchemy.exc.OperationalError: (OperationalError) no such table: registrations 'INSERT INTO registrations SELECT * from migration_tmp' ()
It looks like it's doing a table swap to change the schema. Has anyone else ever run into this? Am I doing something wrong? Or is there a workaround?
EDIT: I'm testing this with sqlite. Here is my manage.py:
from migrate.versioning.shell import main
main(url='sqlite:///development.db', debug='False', repository='migrations')
Using the mysql dialect seems to work fine. Could be isolated to sqlite.