SectorLabs / django-postgres-extra

Bringing all of PostgreSQL's awesomeness to Django.
MIT License
699 stars 96 forks source link

Model is not recognised correctly #249

Open afnan opened 3 months ago

afnan commented 3 months ago

I am not able to go past the error of improper configuration.

    self._partitioning_properties_for_model(model)
  File "/usr/local/lib/python3.12/site-packages/psqlextra/backend/schema.py", line 495, in _partitioning_properties_for_model
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Model 'PlayerGameStats' is not properly configured to be partitioned. Create the `PartitioningMeta` class as a child of 'PlayerGameStats'.
class PlayerGameStats(PostgresPartitionedModel):
    class PartitioningMeta:
        method = PostgresPartitioningMethod.LIST
        key = ["season_id", "competition_id"]

    player      = models.ForeignKey(Player, on_delete=models.CASCADE)
    statistic   = models.ForeignKey(Statistic, on_delete=models.CASCADE)
    season      = models.ForeignKey(Season, on_delete=models.CASCADE)
    competition = models.ForeignKey(Competition, on_delete=models.CASCADE)
    team        = models.ForeignKey(Team, on_delete=models.CASCADE)
    position    = models.ForeignKey(Position, on_delete=models.CASCADE)
    game        = models.ForeignKey(Game, on_delete=models.CASCADE)
    round       = models.ForeignKey(Round, on_delete=models.CASCADE)

    statistic_value = models.DecimalField(null=True, max_digits=10, decimal_places=2)
    rank = models.SmallIntegerField(default=0, blank=True)
    relative = models.DecimalField(null=True, max_digits=10, decimal_places=2)
    is_starred = models.BooleanField(default=False)

    class Meta:
        unique_together = (
            "player",
            "competition",
            "season",
            "round",
            "statistic",
            "team",
            "position",
            "game",
        )       

this is my migration

from django.db import migrations
from psqlextra.backend.migrations.operations import PostgresAddListPartition
from django.db.migrations.state import ProjectState
def create_partitions(apps, schema_editor):
    CompetitionSeason = apps.get_model("competitions", "CompetitionSeason")
    competition_seasons = CompetitionSeason.objects.values_list('competition_id', 'season_id').distinct()

    for competition_id, season_id in competition_seasons:
        partition_name = f"playergamestats_{season_id}_{competition_id}"
        operation = PostgresAddListPartition(
            model_name="playergamestats",
            name=partition_name,
            values=[season_id, competition_id],
        )
        state = ProjectState.from_apps(apps)

        operation.database_forwards(
            app_label='game',
            schema_editor=schema_editor,
            from_state=state,
            to_state=state
        )

class Migration(migrations.Migration):

    dependencies = [
        ('game', '0044_alter_playergamestats_managers'),
    ]

    operations = [       
        migrations.RunPython(create_partitions),
    ]
afnan commented 3 months ago

If i try simple migration

class Migration(migrations.Migration):

    dependencies = [
        ('game', '0044_alter_playergamestats_managers'),
    ]

    operations = [       
        # migrations.RunPython(create_partitions),
        PostgresAddListPartition(
           model_name="playergamestats",
           name="pt1",
           values=["2022", "111"],
        ),
    ]
            ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/db/migrations/executor.py", line 252, in apply_migration
    state = migration.apply(state, schema_editor)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/db/migrations/migration.py", line 118, in apply
    operation.state_forwards(self.app_label, project_state)
  File "/usr/local/lib/python3.12/site-packages/psqlextra/backend/migrations/operations/add_list_partition.py", line 30, in state_forwards
    model.add_partition(
    ^^^^^^^^^^^^^^^^^^^
AttributeError: 'ModelState' object has no attribute 'add_partition'