假设我有一个简单的表格。在原始 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 useVACUUM
to rewrite the table, which will respect the newfillfactor
setting.
前面的引用摘自here
关于python - 如何在sqlalchemy中指定表的填充因子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65741211/