【发布时间】:2016-06-25 18:08:49
【问题描述】:
由于某种原因,当我尝试将我的 Actions 表的某个方面与我的联系人表链接时,我得到一个外键错误。我得到的错误是:
sqlalchemy.exc.AmbiguousForeignKeysError:无法确定关系 Contacts.actions 上的父/子表之间的连接条件 - 有多个外键路径链接表。指定“foreign_keys”参数,提供那些列的列表 应该算作包含对父表的外键引用。
我需要帮助了解为什么已经链接的表不适用于第二个属性。(请参阅下面代码中被 cmets 包围的行)
我的声明代码:
Base = declarative_base()
class Users(Base):
__tablename__ = 'users'
# Here we define columns for the table person
# Notice that each column is also a normal Python instance attribute.
id = Column(Integer, primary_key=True)
username = Column(String(30), nullable=False)
first_name = Column(String(20), nullable=False)
last_name = Column(String(20), nullable=False)
email = Column(String(40), nullable=False)
phone = Column(String(10), nullable=False)
#default_duration = Column(Integer(2))
class Actions(Base):
__tablename__ = 'actions'
id = Column(Integer, primary_key=True)
action = Column(String(15), nullable=False)
message = Column(String(140), nullable=False)
duration = Column(Integer(2), nullable=False)
class Contacts(Base):
__tablename__ = 'contacts'
# Here we define columns for the table address.
# Notice that each column is also a normal Python instance attribute.
id = Column(Integer, primary_key=True)
contact_name = Column(String(30), nullable=False)
time_of_last_message = Column(String(40))
message_log = Column(String(450))
users_id = Column(Integer, ForeignKey('users.id'), nullable=False)
users = relationship(Users)
##############################
#### If this line is commented it out, it works
#### But If it is present I get the error
actions_action = Column(String, ForeignKey('actions.action'))
####
###############################
actions_id = Column(Integer, ForeignKey('actions.id'))
actions = relationship(Actions)
【问题讨论】:
标签: python sqlalchemy foreign-keys