python - 如何在sqlalchemy中指定表的填充因子?

标签 python postgresql sqlalchemy orm alembic

假设我有一个简单的表格。在原始 SQL 中,它看起来像这样:

CREATE TABLE events (id INT PRIMARY KEY) WITH (fillfactor=60);

我的问题是如何使用sqlalchemy声明性基础为表指定填充因子
当然,我可以通过在 sqlalchemy 中使用原始 SQL 来实现这一点,例如 execute("ALTER TABLE events SET (fillfactor=60)"),但我感兴趣是否有一种使用 native sqlalchemy 工具来做到这一点的方法。

我尝试了以下方法,但没有成功:

from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class SimpleExampleTable(Base):
    __tablename__ = 'events'
    __table_args__ = {'comment': 'events table', "fillfactor": 60}

    id = Column(Integer, primary_key=True)
TypeError: Additional arguments should be named <dialectname>_<argument>, got 'fillfactor'

通过查看文档,我仅找到了有关索引中填充因子使用情况的信息。

我的环境:

  • Python 3.9
  • sqlalchemy 1.3.22
  • PostgreSQL 11.6

最佳答案

Tbh,有同样的问题,我在 SQLAlchemy 文档中发现的唯一与 fillfactor 相关的是索引 ( link to docs ):

PostgreSQL allows storage parameters to be set on indexes. The storage parameters available depend on the index method used by the index. Storage parameters can be specified on Index using the postgresql_with keyword argument:

Index('my_index', my_table.c.data, postgresql_with={"fillfactor": 50})

但似乎没有可以直接为表格设置fillfactor的设置选项。

但是仍然可以选择运行原始 SQL 查询(例如 alembic 迁移):

ALTER TABLE mytable SET (fillfactor = 70);

Note that setting fillfactor on an existing table will not rearrange the data, it will only apply to future inserts. But you can use VACUUM to rewrite the table, which will respect the new fillfactor setting.

前面的引用摘自here

关于python - 如何在sqlalchemy中指定表的填充因子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65741211/

相关文章:

python - 在scoped_session中跨进程边界

python - 添加到 sqlalchemy 映射类非数据库属性

python - Django 获取所有用户

python - 将 matlab 程序转换为等效的 python 代码

sql - 子文件夹中文件的累计数量

sql - 在第一次出现字符后剪切字符串

python - 阻止 nosetests 打印日志信息?

python - 如何解决 pandas 的内存分配问题?

sql - 有效查询包含子字符串的列

python - 如何检查 SQLAlchemy session 是否脏