我有一个changeset脚本,我正在尝试向现有的表列添加外键约束。它看起来像:
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()
当我测试更改时,SQLAlchemy抱怨:
sqlalchemy.exc.OperationalError: (OperationalError) no such table: registrations 'INSERT INTO registrations SELECT * from migration_tmp' ()
看起来它正在进行表交换以更改架构。有没有人碰到这个?难道我做错了什么?或者有解决方法吗?
编辑: 我用sqlite测试这个。这是我的manage.py:
from migrate.versioning.shell import main
main(url='sqlite:///development.db', debug='False', repository='migrations')
使用mysql方言似乎工作正常。可以隔离到sqlite。