I am trying to represent clubs and users in a web app using SQLAlchemy. Users must have a list of clubs, and clubs must have a list of users. I think I have done this correctly with a many-to-many relationship, since users can be in multiple clubs. However, I cannot make a club leader. The idea is that a leader should still be able to lead multiple clubs, but each club only has one leader.
I have tried combining different combinations of relationships, but cannot get a leader to be added at all.
user_club_assoc_table = db.Table('user_club_assoc_table',
db.Column('user_id', db.Integer, db.ForeignKey('user.id')),
db.Column('club_id', db.Integer, db.ForeignKey('club.id')))
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
firstname = db.Column(db.String(15), nullable=False)
lastname = db.Column(db.String(15), nullable=False)
email = db.Column(db.String(60), unique=True, nullable=False)
password = db.Column(db.String(60), nullable=False)
clubs = db.relationship('Club', secondary=user_club_assoc_table)
class Club(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), unique=True, nullable=False)
members = db.relationship('User', secondary=user_club_assoc_table)
# This is what is needed
# preferably be able to get all clubs being led by leader
leader = db.relationship(...)
I expect to be able to create users and create clubs. A club should take a user that will be a leader as an argument when it is instantiated.