Why Uniqueconstraint Doesn't Work In Flask_sqlalchemy
I want an alternative of Django's unique_together in flask, seems UniqueConstraint is what I'm looking for, but doesn't work for me. here is the example: import os from flask impor
Solution 1:
An instance of UniqueConstraint
is iterable and in this case seems to stop iteration immediately, so
tuple(db.UniqueConstraint('name', 'address'))
results in an empty tuple, when you wanted a tuple that contains 1 item, the constraint instance. Use
__table_args__ = (db.UniqueConstraint('name', 'address'), )
or any other variation instead. As to why the latter form does not work, you must apply table-level constraint objects using __table_args__
in declarative.
Solution 2:
Passing variable names ( instead of strings ) worked for me.
classUser(db.Model):
__tablename__ = 'users'id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64), nullable=False)
address = db.Column(db.String(64), nullable=False)
db.UniqueConstraint(name, address)
Post a Comment for "Why Uniqueconstraint Doesn't Work In Flask_sqlalchemy"