Gal*_*ses 3 postgresql sqlalchemy
我正在尝试在 SQLAlchemy ORM 中创建一个表,我需要在其中指定模式名称(对于 postgres)和一些约束。要仅指定模式名称,可以使用字典:
class NewTable(Base):
    __tablename__ = "new_table"
    __table_args__ = {"schema": "schema_name"}
    id = Column(Integer, primary_key=True)
    name = Column(String(255), unique=True)
并定义约束,代码如下:
class NewTable(Base):
    __tablename__ = "new_table"
   
    id = Column(Integer)
    name = Column(String(255))
    __table_args__ = (
        PrimaryKeyConstraint("id", name="id_pk"),
        UniqueConstraint("name")
    )
,使用元组。
有人知道如何使用元组语法在最后一个代码块中设置模式名称吗?
我在文档中找到了答案:
https://docs.sqlalchemy.org/en/14/orm/declarative_tables.html#orm-declarative-table-configuration
基本上,所有关键字参数都必须设置在约束元组的末尾:
class NewTable(Base):
    __tablename__ = "new_table"
   
    id = Column(Integer)
    name = Column(String(255))
    __table_args__ = (
        PrimaryKeyConstraint("id", name="id_pk"),
        UniqueConstraint("name"),
        {"schema": "schema_name"}
    )
| 归档时间: | 
 | 
| 查看次数: | 1200 次 | 
| 最近记录: |