Flask-SQLAlchemy外键链接

时间:2014-05-29 10:53:51

标签: python flask

嗨!我正在尝试使用Flask-SQLAlchemy链接两个表:用户和帖子以及Flask中的外键约束。但是,当我尝试创建User类的新实例时,我收到以下错误:

sqlalchemy.exc.NoForeignKeysError: Could not determine join condition between pa
rent/child tables on relationship User.posts - there are no foreign keys linking
 these tables.  Ensure that referencing columns are associated with a ForeignKey
 or ForeignKeyConstraint, or specify a 'primaryjoin' expression.

以下是相关的代码段:

class User(db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    posts = db.relationship('Post', backref='author', lazy='dynamic')

class Post(db.Model):
    __tablename__ = 'posts'
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))

我知道网站上还有其他帖子讨论同一个异常,但是在我的案例中似乎没有一个解决方案可行。非常感谢您的时间:))

0 个答案:

没有答案
相关问题