fix: make playerseason uniqueness context-aware

This commit is contained in:
2026-04-06 19:30:55 +02:00
parent aa283a1235
commit 065a5f66f1
2 changed files with 29 additions and 2 deletions

View File

@ -0,0 +1,21 @@
# Generated by Django 5.2.2 on 2026-04-06 17:30
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('scouting', '0003_alter_player_position'),
]
operations = [
migrations.RemoveConstraint(
model_name='playerseason',
name='uniq_player_season',
),
migrations.AddConstraint(
model_name='playerseason',
constraint=models.UniqueConstraint(fields=('player', 'season', 'team', 'competition'), name='uniq_player_season_context', nulls_distinct=False),
),
]

View File

@ -121,11 +121,17 @@ class PlayerSeason(models.Model):
class Meta: class Meta:
ordering = ["player__full_name", "-season__start_year"] ordering = ["player__full_name", "-season__start_year"]
constraints = [ constraints = [
models.UniqueConstraint(fields=["player", "season"], name="uniq_player_season"), models.UniqueConstraint(
fields=["player", "season", "team", "competition"],
name="uniq_player_season_context",
nulls_distinct=False,
),
] ]
def __str__(self) -> str: def __str__(self) -> str:
return f"{self.player.full_name} - {self.season.name}" team_name = self.team.name if self.team else "No team"
competition_name = self.competition.name if self.competition else "No competition"
return f"{self.player.full_name} - {self.season.name} - {team_name} - {competition_name}"
class PlayerSeasonStats(models.Model): class PlayerSeasonStats(models.Model):