Based on the answer to sqlalchemy unique across multiple columns (I'm using the declarative version, given below) I'm trying to declare a multi-column uniqueness constraint.
class Location(Base):
__tablename__ = 'locations'
id = Column(Integer, primary_key = True)
customer_id = Column(Integer, ForeignKey('customers.customer_id'), nullable=False)
location_code = Column(Unicode(10), nullable=False)
__table_args__ = (UniqueConstraint('customer_id', 'location_code', name='_customer_location_uc'),
)
However when I create multiple items with the same customer_id and location_code I don't get any uniqueness exceptions from SQLAlchemy, and the items are successfully created.
In the comments below the linked answer, there's a partial conversation about specifying UniqueConstraint being only part of the DDL. What's the other part that I need to do for this constraint to get enforced?