You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am refactoring an existing app I created to support table partitioning. The table that needs partitioning will have 100s of millions into the billions of rows and have substantial row size. Right now I am trying to implement this simple example below in a totally separate/unrelated sandbox environment.
fromdjango.dbimportmodelsfrompsqlextra.typesimportPostgresPartitioningMethodfrompsqlextra.modelsimportPostgresPartitionedModel# This represents a table of codes. In production there will be ~10 , not 3, and they are all uniqueclassPlatform(models.Model):
code_options= [("na1","na1"), ("euw1","euw1"), ("br1","br1")]
code=models.CharField(choices=code_options, primary_key=True)
# This is a table of events occurring, in prod there will be up to billions. A Platform will have many Matches# Want to partition by platform which is a foreign keyclassMatch(PostgresPartitionedModel):
classPartitioningMeta:
method=PostgresPartitioningMethod.LISTkey= ["platform"]
matchId=models.CharField(max_length=20)
platform=models.ForeignKey(Platform, on_delete=models.CASCADE)
Problem:
I want to partition Matches based on the platform it belongs to. There are a fixed number of options so I want to use list partitioning. This seems reasonable to me because I want "parent" level & also partition level reporting. I don't think what I'm trying to do is controversial and I would like to not make ~10 separate tables separate that are almost identical.
I run python3 manage.py pgmakemigrations and my migration is created:
I think this is happening because Django/PSQL thinks that my Match model does not have the field platform yet when it tries to create the partition. I noticed when I create the migration the order of operations is:
Migrations for 'wrs_api':
wrs_api/migrations/0001_initial.py
- Create partitioned model Match
- Creates default partition 'default' on Match
- Create model Platform
- Add field platform to match
The platform field is not getting added to the Match model/table until after Match tries to partition itself by platform. It makes sense why this is an error. If I change my partion key to key = ["foobar"] I get the the exact same error language....
...but if I manually change my migration to create my generic model Platform first & I manually add the platform field with a foreign key constraint to Match in the .PostgresCreatePartitionedModel() config like so:
Tool versions:
Scenario:
I am refactoring an existing app I created to support table partitioning. The table that needs partitioning will have 100s of millions into the billions of rows and have substantial row size. Right now I am trying to implement this simple example below in a totally separate/unrelated sandbox environment.
Problem:
I want to partition
Matches
based on the platform it belongs to. There are a fixed number of options so I want to use list partitioning. This seems reasonable to me because I want "parent" level & also partition level reporting. I don't think what I'm trying to do is controversial and I would like to not make ~10 separate tables separate that are almost identical.I run
python3 manage.py pgmakemigrations
and my migration is created:Click to expand
Error when I migrate:
Analysis / Trying to Fix:
I think this is happening because Django/PSQL thinks that my
Match
model does not have the fieldplatform
yet when it tries to create the partition. I noticed when I create the migration the order of operations is:The
platform
field is not getting added to theMatch
model/table until afterMatch
tries to partition itself byplatform
. It makes sense why this is an error. If I change my partion key tokey = ["foobar"]
I get the the exact same error language.......but if I manually change my migration to create my generic model
Platform
first & I manually add theplatform
field with a foreign key constraint toMatch
in the.PostgresCreatePartitionedModel()
config like so:Click to see manually altered migration
I get this error instead:
TL;DR:
isn't this a normal use case? And if so how do you partition a table by a foreign key field
The text was updated successfully, but these errors were encountered: