I need to create a table called friends, it should looks like:
friends:
- user_id
- friend_id
I was trying to do this with tutorials from SQLALchemy, but I have not found how to make relation many-to-many for same table.
Here's what I have tried:
# 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
)
it says:
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.
does anyone know how to do that properly?