英文:
How to create a partitioned table with Peewee?
问题
当使用Peewee定义数据模型时,如何按照给定字段进行分区?我想按照日期列进行分区,例如,按照每年的月份进行分区。
我在Peewee的文档中看到可以在数据模型的Meta部分添加自定义约束,那么如何添加分区命令,该命令将放在表创建DDL的末尾?
是否可以在架构创建后执行此操作?这也是一种方法...
英文:
When defining my data model with peewee, how can I make a table partitioned by a given field? I’m interested in partitioning a table by a date column, for example, by month of the year.
I see in Peewee’s documentation that I can add custom constraints as part of the Meta part of the data model, how can I then add then partitioning command which would go at the end of the table creation DDL?
Is there a way to do it post schema creation? This would also be a way to go …
答案1
得分: 2
我在我的废弃目录中找到了一些代码,这些代码可能会对你有所帮助 - 这里我有一个文档表,根据“is_reviewed”列对文档进行了分区:
db = PostgresqlDatabase('parttest')
class Base(Model):
class Meta:
database = db
class Document(Base):
# 我认为这样做是必要的,而不是使用SERIAL。
id = IntegerField(sequence='document_id_seq', index=True)
name = CharField()
is_reviewed = BooleanField()
class Meta:
table_settings = 'PARTITION BY LIST (is_reviewed)'
# 用于直接访问分区的文档子类。
class DocumentReviewed(Document):
class Meta:
table_name = 'document_reviewed'
class DocumentUnreviewed(Document):
class Meta:
table_name = 'document_unreviewed'
db.create_tables([Document])
# 创建分区。
db.execute_sql('create table document_reviewed partition of document '
'for values in (true)')
db.execute_sql('create table document_unreviewed partition of document '
'for values in (false)')
希望这对你有帮助。
英文:
I found some code in my scrap directory that may help you - here I've got a document table that is partitioned into docs that are reviewed/un-reviewed (based on the "is_reviewed" column):
db = PostgresqlDatabase('parttest')
class Base(Model):
class Meta:
database = db
class Document(Base):
# I believe it is necessary to do it this way vs using SERIAL.
id = IntegerField(sequence='document_id_seq', index=True)
name = CharField()
is_reviewed = BooleanField()
class Meta:
table_settings = 'PARTITION BY LIST (is_reviewed)'
# Doc subclasses for accessing partitions directly.
class DocumentReviewed(Document):
class Meta:
table_name = 'document_reviewed'
class DocumentUnreviewed(Document):
class Meta:
table_name = 'document_unreviewed'
db.create_tables([Document])
# Create the partitions.
db.execute_sql('create table document_reviewed partition of document '
'for values in (true)')
db.execute_sql('create table document_unreviewed partition of document '
'for values in (false)')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论