【问题标题】:How can I add a custom, arbitrary option to a table in sqlalchemy?如何向 sqlalchemy 中的表添加自定义的任意选项?
【发布时间】:2019-11-01 21:30:00
【问题描述】:

我正在尝试使用 sqlalchemy 的 declarative_base 创建一个表,我想添加 cockroachdbINTERLEAVE IN PARENT 选项:

CREATE TABLE orders (
    customer INT,
    id INT,
    total DECIMAL(20, 5),
    PRIMARY KEY (customer, id),
    CONSTRAINT fk_customer FOREIGN KEY (customer) REFERENCES customers
  ) INTERLEAVE IN PARENT customers (customer);

如何将它添加到 DDL?

【问题讨论】:

    标签: python sqlalchemy cockroachdb


    【解决方案1】:

    等待 cockroachdb 方言正式实施,您可以自行扩充以实施所需的选项:

    from sqlalchemy import Table, util
    from sqlalchemy.schema import CreateTable
    from sqlalchemy.ext.compiler import compiles
    
    Table.argument_for("cockroachdb", "interleave_in_parent", None)
    
    @compiles(CreateTable, "cockroachdb")
    def compile_create_table(create, compiler, **kw):
        preparer = compiler.preparer
        stmt = compiler.visit_create_table(create, **kw)
        cockroachdb_opts = create.element.dialect_options["cockroachdb"]
        interleave = cockroachdb_opts.get("interleave_in_parent")
    
        if interleave:
            p_tbl, c_cols = interleave
    
            parent_tbl = preparer.format_table(p_tbl)
            child_cols = ", ".join([ 
                preparer.quote(c)
                if isinstance(c, util.string_types) else
                preparer.format_column(c)
                for c in c_cols
            ])
    
            stmt = stmt.rstrip()  # Prettier output, remove newlines
            stmt = f"{stmt} INTERLEAVE IN PARENT {parent_tbl} ({child_cols})\n\n"
    
        return stmt
    

    然后像这样使用它:

    class Customer(Base):
        ...
    
    class Order(Base):
        customer = Column(...)
        ...
        __table_args__ = {
            "cockroachdb_interleave_in_parent": (Customer.__table__, [customer])
        }
    

    【讨论】:

      猜你喜欢
      • 2021-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-18
      • 1970-01-01
      • 2018-07-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多