I am not sure I correctly named this question. I can add a unique constraint, good enough for any of my tables, but in the example below, I'm not sure how to do what I need:
class Branch(db.Model):
id = db.Column(db.Integer, primary_key = True)
name = db.Column(db.String(160))
account_id = db.Column(db.Integer, db.ForeignKey('account.id'))
class Account(db.Model):
id = db.Column(db.Integer, primary_key = True)
name = db.Column(db.String(160), unique=True)
branches = db.relationship('Branch', backref = 'account', lazy = 'dynamic')
Therefore, when I added a unique constraint to the column column of the Branch table name, I could not add branches with the same name to different accounts. For example, Seattle may be an affiliate for AccountA and AccountB.
What I want to do is apply a constraint that checks for uniqueness only if account.id is the same. Is it possible?