从以下文档的摘录
Column:
unique –为True时,指示此列包含唯一约束,或者,如果 index也
为True,则指示应使用唯一标志创建索引。要在约束/索引中指定多个列或指定一个显式名称,请显式使用
UniqueConstraint或Index构造。
由于这些属于表而不属于映射的类,因此可以在表定义中声明它们,或者如果使用声明性声明,例如
__table_args__:
# version1: table definitionmytable = Table('mytable', meta, # ... Column('customer_id', Integer, ForeignKey('customers.customer_id')), Column('location_pre', Unipre(10)), UniqueConstraint('customer_id', 'location_pre', name='uix_1') )# or the index, which will ensure uniqueness as wellIndex('myindex', mytable.c.customer_id, mytable.c.location_pre, unique=True)# version2: declarativeclass Location(base): __tablename__ = 'locations' id = Column(Integer, primary_key = True) customer_id = Column(Integer, ForeignKey('customers.customer_id'), nullable=False) location_pre = Column(Unipre(10), nullable=False) __table_args__ = (UniqueConstraint('customer_id', 'location_pre', name='_customer_location_uc'), )


