【发布时间】:2012-01-05 21:57:59
【问题描述】:
在user_models.py,我有这个:
class Users(Base):
__tablename__ = 'account_users'
id = Column(Integer, primary_key = True)
username = Column(String(255), nullable=False)
Base.metadata.create_all(engine)
当我运行它时,我会创建一个用户表。
在我的另一个文件 groups_models.py 上,我有这个:
class Groups(Base):
__tablename__ = 'personas_groups'
id = Column(Integer, primary_key = True)
user_id = Column(Integer, ForeignKey('account_users.id')) #This creates an error!!!
user = relationship('Users') #this probably won't work. But haven't hit this line yet.
Base.metadata.create_all(engine)
因此,如您所见,我想从组 -> 用户中建立多对一关系。
但是当我运行groups_models.py...我得到这个错误:
sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column 'personas_groups.user_id' could not find table 'account_users' with which to generate a foreign key to target column 'id'
如果我将两个表放在一个文件中,我相信它可以工作...但是因为我将它分成 2 个文件(我绝对必须这样做)...我不知道如何制作ForeignKey 关系不再起作用了?
【问题讨论】:
标签: mysql database orm sqlalchemy