SQLAlchemy alembic AmbiguousForeignKeysError 用于声明类型但不用于等效的非声明类型
SQLAlchemy alembic AmbiguousForeignKeysError for declarative type but not for equivalent non-declarative type
我有以下 alembic 迁移:
revision = '535f7a49839'
down_revision = '46c675c68f4'
from alembic import op
import sqlalchemy as sa
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from datetime import datetime
Session = sessionmaker()
Base = declarative_base()
metadata = sa.MetaData()
# This table definition works
organisations = sa.Table(
'organisations',
metadata,
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('creator_id', sa.Integer),
sa.Column('creator_staff_member_id', sa.Integer),
)
"""
# This doesn't...
class organisations(Base):
__tablename__ = 'organisations'
id = sa.Column(sa.Integer, primary_key=True)
creator_id = sa.Column(sa.Integer)
creator_staff_member_id = sa.Column(sa.Integer)
"""
def upgrade():
bind = op.get_bind()
session = Session(bind=bind)
session._model_changes = {} # if you are using Flask-SQLAlchemy, this works around a bug
print(session.query(organisations).all())
raise Exception("don't succeed")
def downgrade():
pass
现在,当我使用命令式定义的 table(未注释掉的那个)时,查询 session.query(organisations).all()
有效。但是,如果我使用声明式版本(据我所知应该是等效的),则会出现错误:
sqlalchemy.exc.AmbiguousForeignKeysError: Could not determine join
condition between parent/child tables on relationship
StaffMember.organisation - there are multiple foreign key paths
linking the tables. Specify the 'foreign_keys' argument, providing a
list of those columns which should be counted as containing a foreign
key reference to the parent table.
现在我明白这个错误的意思了:我的 实际模型 中有两个从 organisations
到 staff_members
的外键。但是为什么 alembic 关心这些,它怎么知道它们的存在呢?此迁移如何知道存在名为 StaffMember
的东西?据我所知,alembic 应该只知道你在迁移中明确告诉它的模型。
原来问题出在我用来调用 alembic 的 Flask-script 设置上。我用来调用 alembic 的命令是导入代码来初始化我的 Flask 应用程序,它本身正在导入我的实际模型。
我有以下 alembic 迁移:
revision = '535f7a49839'
down_revision = '46c675c68f4'
from alembic import op
import sqlalchemy as sa
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from datetime import datetime
Session = sessionmaker()
Base = declarative_base()
metadata = sa.MetaData()
# This table definition works
organisations = sa.Table(
'organisations',
metadata,
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('creator_id', sa.Integer),
sa.Column('creator_staff_member_id', sa.Integer),
)
"""
# This doesn't...
class organisations(Base):
__tablename__ = 'organisations'
id = sa.Column(sa.Integer, primary_key=True)
creator_id = sa.Column(sa.Integer)
creator_staff_member_id = sa.Column(sa.Integer)
"""
def upgrade():
bind = op.get_bind()
session = Session(bind=bind)
session._model_changes = {} # if you are using Flask-SQLAlchemy, this works around a bug
print(session.query(organisations).all())
raise Exception("don't succeed")
def downgrade():
pass
现在,当我使用命令式定义的 table(未注释掉的那个)时,查询 session.query(organisations).all()
有效。但是,如果我使用声明式版本(据我所知应该是等效的),则会出现错误:
sqlalchemy.exc.AmbiguousForeignKeysError: Could not determine join condition between parent/child tables on relationship StaffMember.organisation - there are multiple foreign key paths linking the tables. Specify the 'foreign_keys' argument, providing a list of those columns which should be counted as containing a foreign key reference to the parent table.
现在我明白这个错误的意思了:我的 实际模型 中有两个从 organisations
到 staff_members
的外键。但是为什么 alembic 关心这些,它怎么知道它们的存在呢?此迁移如何知道存在名为 StaffMember
的东西?据我所知,alembic 应该只知道你在迁移中明确告诉它的模型。
原来问题出在我用来调用 alembic 的 Flask-script 设置上。我用来调用 alembic 的命令是导入代码来初始化我的 Flask 应用程序,它本身正在导入我的实际模型。