我对SQLAlchemy相当陌生,我想知道是否有人可以帮助我破译这个错误消息:
ERROR::sqlalchemy.exc.ArgumentError: Can't create ForeignKeyConstraint on table 'attacker_battles': no column named 'battles.battle_id' is present.
它在Battles
表中执行。在
我的代码:
class Battles(Base):
__tablename__ = 'battles'
battle_id = Column(Integer, primary_key=True)
starttime = Column(DateTime)
endtime = Column(DateTime)
class Attacker_Battles(Base):
__tablename__ = 'attacker_battles'
__table_args__ = (
ForeignKeyConstraint(
['battle_id','battles.battle_id'],
['attacker_id','player.player_id']),
UniqueConstraint('battle_id','attacker_id', 'player_id', name= 'attacking_player_unique_id')
)
attacker_id = Column(Integer, primary_key=True)
battle_id = Column(Integer, primary_key=True)
player_id = Column(Integer, primary_key=True)
first argument到{a2}应该是本地列的序列,the second应该是引用的外部列的匹配序列。现在你有两个本地的,外国的配对列表。在
但是:您试图创建一个引用2个或更多表的复合外键约束,这在SQL中是不可能的。仔细想想,原因就很清楚了:什么key会跨越多个表?在
相反,您可以为
battle_id
和attacker_id
创建单独的外键约束,如果这适合您的模型:另外,由于您已经定义主键由3列组成,所以unique约束是多余的。在
相关问题 更多 >
编程相关推荐