两个表之间的关系,SQLAlchemy
Relationship between two tables, SQLAlchemy
我想在 AuthorComments 和 Reply to his comments 之间建立关系。
这是我的models.py:
class AuthorComments(Base):
id = db.Column(db.Integer, primary_key=True)
author_id = db.Column(db.Integer, db.ForeignKey('author.id'))
name = db.Column(db.String(50))
email = db.Column(db.String(50), unique=True)
comment = db.Column(db.Text)
live = db.Column(db.Boolean)
comments = db.relationship('Reply', backref='reply', lazy='joined')
def __init__(self,author, name, email, comment, live=True):
self.author_id = author.id
self.name = name
self.email = email
self.comment = comment
self.live = live
class Reply(Base):
id = db.Column(db.Integer, primary_key=True)
reply_id = db.Column(db.Integer, db.ForeignKey('author.id'))
name = db.Column(db.String(50))
email = db.Column(db.String(50), unique=True)
comment = db.Column(db.Text)
live = db.Column(db.Boolean)
def __init__(self,author, name, email, comment, live=True):
self.reply_id = author.id
self.name = name
self.email = email
self.comment = comment
self.live = live
为什么我会收到此错误:
sqlalchemy.exc.InvalidRequestError
InvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization of other mappers. Original exception was: Could not determine join condition between parent/child tables on relationship AuthorComments.comments - there are no foreign keys linking these tables. Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify a 'primaryjoin' expression.
你的问题是 SQLAlchemy 不知道,对于给定的子行 table (Reply
),父行 table (AuthorComments
) 到 select!您需要在 Reply
中定义一个 foreign-key 列,该列引用其父项 AuthorComments
.
的列
Here 是关于在 SQLAlchemy 中定义一对多关系的文档。
像这样:
class AuthorComments(Base):
__tablename__ = 'author_comment'
...
class Reply(Base):
...
author_comment_id = db.Column(db.Integer, db.ForeignKey('author_comment.id'))
...
author_comment = db.relationship(
'AuthorComments',
backref='replies',
lazy='joined'
)
将导致每个 reply
获得与 author_comment
的关系,这样 some_reply.author_comment_id == some_author_comment.id
或 None
如果不存在这样的等式。
backref
允许每个 author_comment
与一个名为 replies
的回复集合相互关联,满足上述条件。
我想在 AuthorComments 和 Reply to his comments 之间建立关系。
这是我的models.py:
class AuthorComments(Base):
id = db.Column(db.Integer, primary_key=True)
author_id = db.Column(db.Integer, db.ForeignKey('author.id'))
name = db.Column(db.String(50))
email = db.Column(db.String(50), unique=True)
comment = db.Column(db.Text)
live = db.Column(db.Boolean)
comments = db.relationship('Reply', backref='reply', lazy='joined')
def __init__(self,author, name, email, comment, live=True):
self.author_id = author.id
self.name = name
self.email = email
self.comment = comment
self.live = live
class Reply(Base):
id = db.Column(db.Integer, primary_key=True)
reply_id = db.Column(db.Integer, db.ForeignKey('author.id'))
name = db.Column(db.String(50))
email = db.Column(db.String(50), unique=True)
comment = db.Column(db.Text)
live = db.Column(db.Boolean)
def __init__(self,author, name, email, comment, live=True):
self.reply_id = author.id
self.name = name
self.email = email
self.comment = comment
self.live = live
为什么我会收到此错误: sqlalchemy.exc.InvalidRequestError
InvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization of other mappers. Original exception was: Could not determine join condition between parent/child tables on relationship AuthorComments.comments - there are no foreign keys linking these tables. Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify a 'primaryjoin' expression.
你的问题是 SQLAlchemy 不知道,对于给定的子行 table (Reply
),父行 table (AuthorComments
) 到 select!您需要在 Reply
中定义一个 foreign-key 列,该列引用其父项 AuthorComments
.
Here 是关于在 SQLAlchemy 中定义一对多关系的文档。
像这样:
class AuthorComments(Base):
__tablename__ = 'author_comment'
...
class Reply(Base):
...
author_comment_id = db.Column(db.Integer, db.ForeignKey('author_comment.id'))
...
author_comment = db.relationship(
'AuthorComments',
backref='replies',
lazy='joined'
)
将导致每个 reply
获得与 author_comment
的关系,这样 some_reply.author_comment_id == some_author_comment.id
或 None
如果不存在这样的等式。
backref
允许每个 author_comment
与一个名为 replies
的回复集合相互关联,满足上述条件。