我正在玩一个玩具示例,看看背面是否有效,但遇到了我无法理解的错误。下面我有两个型号'后面相互填充。当我尝试创建一个User对象时,它会抛出一个错误。我错过了什么?
" sqlalchemy.exc.NoForeignKeysError:无法确定关系Child.user上的父/子表之间的连接条件 - 没有链接这些表的外键。确保引用列与ForeignKey或ForeignKeyConstraint相关联,或指定' primaryjoin' 。表达"
engine = create_engine('sqlite:///:memory:', echo=False)
Session = sessionmaker(bind=engine)
session = Session()
Base = declarative_base(bind=engine)
class User(Base):
__tablename__ = 'user'
user_id = Column("UserID", Integer, primary_key=True)
name = Column("Name", String(50))
age = Column("Age", SmallInteger)
child = relationship("Child", back_populates="user")
class Child(Base):
__tablename__ = 'child'
child_id = Column("ChildID", Integer, primary_key=True)
school = Column("School", String)
grade = Column("Grade", String)
user_id = Column("UserID", Integer, ForeignKey('User.UserID'), index=True, nullable=True)
user = relationship("User", back_populates="child")
答案 0 :(得分:1)
user_id = Column("UserID", Integer, ForeignKey('user.UserID'), ...)
需要表名和列名,而不是模型和属性名,所以它应该是:
User
因为您的user
模型的表名为Oauth2
。