How can I add a custom, arbitrary option to a table in sqlalchemy?

落花浮王杯 提交于 2020-03-20 06:20:06

问题


I'm trying to create a table with sqlalchemy's declarative_base and I would like to add cockroachdb's INTERLEAVE IN PARENT option:

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);

How could I add that to the DDL?


回答1:


Pending official implementation by the cockroachdb dialect, you can augment it yourself to implement the required options:

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

And then use it like:

class Customer(Base):
    ...

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


来源:https://stackoverflow.com/questions/56662626/how-can-i-add-a-custom-arbitrary-option-to-a-table-in-sqlalchemy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!