SQLAlchemy 多列约束

SQLAlchemy multi column constraint

我有许多不同模式的表,我使用了 docs. Some of my tables require multi column constraints and it was unclear what the dictionary key would be for declaring that unique constraint as they mention in the section above

中的模式

在我下面的模型中,我想用 name, key, org 创建一个唯一约束。我目前必须在 sql...

中执行此操作
class Parent(Base):
  __tablename__ = 'parent'
  __table_args__ = {'schema': 'example'}

  id = Column(Integer, primary_key=True)
  name = Column(String(512))
  key = Column(String())
  org = Column(String(36))

我想我前段时间遇到过这个问题。如果我没记错的话,这只是将“模式字典”移动到一个也包含您的约束的元组内部的问题。

如果这不起作用,我可以尝试进一步挖掘,但文档似乎同意使用 declarative table configuration via __table_args__ can be a tuple containing positional arguments (like constraints) and as a final argument a dict with keyword arguments like schema is for Table.

class Parent(Base):
    __tablename__ = 'parent'
    __table_args__ = (
        UniqueConstraint('name', 'key', 'org'),
        {'schema': 'example'},
    )

    id = Column(Integer, primary_key=True)
    name = Column(String(512))
    key = Column(String())
    org = Column(String(36))