Python 3.x SQLAlchemy:ORM用于自动分区表(postgres)

Python 3.x SQLAlchemy:ORM用于自动分区表(postgres),python-3.x,postgresql,sqlalchemy,Python 3.x,Postgresql,Sqlalchemy,我想把我的代码重写成ORM,它将执行以下操作 具有分区表: 插入之前将创建新分区的位置 插入后,将设置表上的索引 我现在看到的是python和SQL的混合: def execute_query(query): with psycopg2.connect(**db) as conn: conn.autocommit = True with conn.cursor() as cur: cur.execute(query) SCHEMA

我想把我的代码重写成ORM,它将执行以下操作

具有分区表:

  • 插入之前将创建新分区的位置
  • 插入后,将设置表上的索引
  • 我现在看到的是python和SQL的混合:

    def execute_query(query):
        with psycopg2.connect(**db) as conn:
            conn.autocommit = True
            with conn.cursor() as cur:
                cur.execute(query)
    
    SCHEMA='testing'
    
    # One Time
    execute_query(f'''
        CREATE TABLE {SCHEMA}.partitioned_table (
            name TEXT,
            report_date DATE NOT NULL)
        PARTITION BY LIST (report_date)
    ''')
    
    # Everyday
    for report_date in ['2019-01-01', '2019-02-01', '2019-03-01']:
        #create partition
        execute_query(f"CREATE TABLE IF NOT EXISTS {SCHEMA}.partitioned_table_p{report_date.replace('-', '')} PARTITION OF {SCHEMA}.partitioned_table FOR VALUES IN ('{report_date}')")
        #insert data into that table
        # some additional code
        #create index on that partition
        execute_query(f'''CREATE INDEX IF NOT EXISTS ix_name ON {SCHEMA}.partitioned_table_p{report_date.replace('-', '')} (name);''')
    
    这就是我现在要写的:

    Base = declarative_base()
    Base.metadata.schema = 'testing'
    
    class PartitionedTable(Base):
    
        __tablename__ = 'partitioned_table'
    
        name = Column(String, nullable=False)
        report_date = Column(Date, nullable=False)
        # Here I'm lacking some argument like:
        #__table_args__ = {postgresql_partition_by: 'LIST (report_date)'}
    
        def __repr__(self):
            return '<PartitionedTable>(%r, %r)' % (self.name, self.report_date)
    
    
    event.listen(
        PartitionedTable,
        "before_insert",
        DDL(f"CREATE TABLE IF NOT EXISTS {SCHEMA}.partitioned_table_p{report_date.replace('-', '')} PARTITION OF {SCHEMA}.partitioned_table FOR VALUES IN ('{report_date}')")
    )
    
    event.listen(
        PartitionedTable,
        "after_insert",
        DDL(f'''CREATE INDEX IF NOT EXISTS ix_name ON {SCHEMA}.partitioned_table_p{report_date.replace('-', '')} (name);''')
    )
    
    Base.metadata.create_all(engine)
    
    Base=declarative_Base()
    Base.metadata.schema='testing'
    类分区表(基):
    __tablename_uuu='分区的_u表'
    名称=列(字符串,可空=False)
    报告日期=列(日期,可空=假)
    #这里我缺少一些论点,比如:
    #__表_args_uuu={postgresql_partition_by:'LIST(report_date)}
    定义报告(自我):
    返回“(%r,%r)”%(self.name,self.report\u date)
    听我说(
    分区表,
    “插入前”,
    DDL(f“如果不存在创建表{SCHEMA}。分区表{report_date。替换{SCHEMA}的('-','')}分区。{report_date}中的值的分区表”)
    )
    听我说(
    分区表,
    “插入后”,
    DDL(f''CREATE INDEX IF note EXISTS ON{SCHEMA}.分区表{report_date.replace('-','')}(name);'')
    )
    Base.metadata.create_all(引擎)
    
    我知道我可能会修改一些sql过程并将其添加为触发器,但我希望尽可能多地将其保留在python中