エキス:
ユニーク - Trueの場合、この列はユニーク 制約が含まれている、またはインデックスが同様にTrueの場合、そのインデックスが を示していることを示しています固有のフラグで作成する必要があります。 に複数の列を指定するには、制約/インデックスを指定するか、明示的な名前を指定するには、明示的に UniqueConstraintまたはIndexの構造を使用します。これらとして
表にしていないマップされたクラスに属し、1は、テーブル定義のものを宣言し、または__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'),
)
出典
2012-04-08 07:26:01
van
私も同じ問題に直面しますが、 UniqueConstraintを使用しても私を助けませんでした。私がIndex( '...')を試した後、私はユニークな制約を得ます。この動作について説明はありますか? – swdev
@swdev:どのRDBMSを使用していますか? – van
私はPostgreSQLを使用しています。これに問題はありますか? – swdev