0
試圖建立一個數據庫,並得到以下錯誤:更多瓶SQL鍊金術,誤差多對多的關係
File "XXXXX/flask/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py", line 1987, in _determine_joins
"expressions." % (self.prop, self.secondary)) NoForeignKeysError: Could not determine join condition between parent/child tables on relationship PostReply.tags - there are no foreign keys linking these tables via secondary table 'postReplyTags'. Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify 'primaryjoin' and 'secondaryjoin' expressions.
一點關於這個數據庫和相關的零部件。它有帖子和帖子回覆,兩者都可以添加標籤。
它是建立類似如下:
postTags = db.Table('postTags',
db.Column('tag_id', db.Integer, db.ForeignKey('tag.id')),
db.Column('post_id', db.Integer, db.ForeignKey('post.id'))
)
postReplyTags = db.Table('postReplyTags',
db.Column('tag_id', db.Integer, db.ForeignKey('tag.id')),
db.Column('postreply_id', db.Integer, db.ForeignKey('postreply.id'))
)
class Post(db.Model):
__searchable__ = ['post_text']
id = db.Column(db.Integer, primary_key=True)
post_text = db.Column(db.String(256))
tags = db.Column(db.String(64))
#post_image: to be added
post_time = db.Column(db.Integer)
post_reply_number = db.Column(db.Integer) #to be taken out
number_of_replies = db.Column(db.Integer)
post_score = db.Column(db.Integer)
post_rank = db.Column(db.Integer)
flagged = db.Column(db.Integer)
admin_post = db.Column(db.Boolean)
company_name = db.Column(db.String(64))
tagged = db.relationship('Tag', secondary=postTags,
backref=db.backref('posts', lazy='dynamic'))
#has user
user_id = db.Column(db.String, db.ForeignKey('user.email'))
def __repr__(self):
return '<User %r>' % self.post_text
def as_dict(self):
return {c.name: getattr(self, c.name) for c in self.__table__.columns}
class PostReply(db.Model):
__searchable__ = ['reply_text']
id = db.Column(db.Integer, primary_key=True)
associated_post_id = db.Column(db.Integer)
reply_text = db.Column(db.String(256))
tags = db.Column(db.String(64))
#post_image: to be added
reply_time = db.Column(db.Integer)
flagged = db.Column(db.Integer)
#has user
user_id = db.Column(db.String, db.ForeignKey('user.email'))
tags = db.relationship('Tag', secondary=postReplyTags,
backref=db.backref('post_replies', lazy='dynamic'))
def __repr__(self):
return '<User %r>' % self.post_text
def as_dict(self):
return {c.name: getattr(self, c.name) for c in self.__table__.columns}
class Tag(db.Model):
id = db.Column(db.Integer, primary_key=True)
post_id = db.Column(db.Integer, db.ForeignKey('post.id'))
postreply_id = db.Column(db.Integer, db.ForeignKey('postreply.id'))
name = db.Column(db.String(64))
奇怪的是,所有上崗的作品,但後答覆沒有。任何人都可以幫我弄清楚爲什麼會發生這種情況?這是標籤和帖子/帖子回覆的多對多關係?非常感謝!