team_id = db.Column(db.Integer, db.ForeignKey('team.id'), primary_key=True)
role_id = db.Column(db.Integer, db.ForeignKey('role.id'), primary_key=True)
db.UniqueConstraint('user_id', 'team_id', 'role_id')
db.relationship('User', uselist=False, backref='memberships', lazy='dynamic')
db.relationship('Team', uselist=False, backref='memberships', lazy='dynamic')
db.relationship('Role', uselist=False, backref='memberships', lazy='dynamic')

def __init__(self, user, team, role):
    self.user_id = user.id
self.team_id = team.id
self.role_id = role.id

def __repr__(self):
    return '<Membership(%s)>'
How to three-way many-to-many relationship in flask-sqlalchemy