从以下文档中提取Column
:
unique – 如果为 True,则表示该列包含唯一约束,或者如果index也为 True,则表示应该使用唯一标志创建索引。要在约束/索引中指定多个列或指定显式名称,请显式使用
UniqueConstraint或Index构造。
由于这些属于表而不是映射类,因此可以在表定义中声明它们,或者如果使用声明性如下__table_args__
:
# version1: table definition
mytable = Table('mytable', meta,
# ...
Column('customer_id', Integer, ForeignKey('customers.customer_id')),
Column('location_code', Unicode(10)),
UniqueConstraint('customer_id', 'location_code', name='uix_1')
)
# or the index, which will ensure uniqueness as well
Index('myindex', mytable.c.customer_id, mytable.c.location_code, unique=True)
# version2: declarative
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'),
)