我需要創建一個表名爲朋友,它應該是這樣的:如何創建模型的用戶關係多對多在SQLAlchemy的(蟒蛇,瓶),以自身
朋友:
- user_id說明
- friend_id
我試圖從SQL教程要做到這一點鍊金術,但我還沒有找到如何在同一張桌子上建立多對多的關係。
這是我曾嘗試:
# friends table
# many to many - user - user
_friends = db.Table('friends',
db.Column('user_id', db.Integer, db.ForeignKey('users.id')),
db.Column('friend_id', db.Integer, db.ForeignKey('users.id'))
)
class User(db.Model, UserMixin):
# table name in database
__tablename__ = 'users'
# primary key for table in db
id = db.Column(db.Integer, primary_key=True)
# email is unique!
email = db.Column(db.String(255), unique=True)
# password, max = 255
password = db.Column(db.String(255))
# category relation
categories = relationship("Category")
# cards relation
cards = relationship("BusinessCard")
# friends
friends = db.relationship(
'User',
backref="users",
secondary=_friends
)
它說:
AmbiguousForeignKeysError: Could not determine join condition between parent/child tables on relationship User.friends - there are multiple foreign key paths linking the tables via secondary table 'friends'. Specify the 'foreign_keys' argument, providing a list of those columns which should be counted as containing a foreign key reference from the secondary table to each of the parent and child tables.
沒有人知道該怎麼做是否正確?
+1你打我吧:) – Miguel
@Miguel哈哈:) – Ang