Skip to content

Partition table with foreign key as key problems #123

@slapshin

Description

@slapshin

Hi! Thank you for the useful tool!
I have some problems/questions about generating migration for partitioned models.

For example we have model:

class MyModel(PostgresPartitionedModel):
    class PartitioningMeta:
        method = PostgresPartitioningMethod.RANGE
        key = ["foreign_field"]

    title = models.CharField(...)
    foreign_field = models.ForeignKey(...)

migration looks like:

    operations = [
         psqlextra.backend.migrations.operations.create_partitioned_model.PostgresCreatePartitionedModel(...),
         psqlextra.backend.migrations.operations.add_default_partition.PostgresAddDefaultPartition(...),
         migrations.AddField(
            model_name='MyModel',
            name='foreign_field',
        ),

So, firstly table with "simple" fields is created and after that foreign field will be added to the table. But if this field is used as a partition key we will have the error:
"Model 'MyModel' is not properly configured to be partitioned. Field 'foreign_field' in partitioning key ['foreign_field'] is not a valid field on 'MyModel'". Because the model doesn't "see" this field on executing the PostgresCreatePartitionedModel operation. Why relations are adding after table creation? Maybe it is better to combine this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions