After a few commits offline including updating the model a couple of times and deployment to production the error message is: (It is working for development but failing in production)
django.db.utils.IntegrityError: could not create unique index - DETAIL: Key (player)=(Lonergan) is duplicated.
It is over 100 players with a duplicate name in the table and the field player was never set to be unique. Why is this happening?
class Player(models.Model):
player = models.CharField(max_length=50)
team = models.ForeignKey(Team, related_name='players', on_delete=models.PROTECT)
position = models.CharField(max_length=5)
cost = models.FloatField()
selection = models.FloatField()
form = models.FloatField()
points = models.IntegerField()
lastgwscrape = models.DateTimeField(null=True)
lastapiupdate = models.DateTimeField(null=True)
currentgwdt = models.DateTimeField(null=True)
apiid = models.IntegerField(null=True)
The apiid field was previously defined as unique and it was removed as a test to make this migration work.
class APIPlayerGW(models.Model):
player = models.ForeignKey(Player, related_name='apigws', on_delete=models.CASCADE)
gwid = models.IntegerField()
points = models.IntegerField()
minutesplayed = models.IntegerField()
goalsscored = models.IntegerField()
assists = models.IntegerField()
cleansheets = models.IntegerField()
goalsconceded = models.IntegerField()
owngoals = models.IntegerField()
penaltiessaved = models.IntegerField()
penaltiesmissed = models.IntegerField()
yellowcards = models.IntegerField()
redcards = models.IntegerField()
saves = models.IntegerField()
bonuspoints = models.IntegerField()
bonuspointsystem = models.IntegerField()
influence = models.FloatField()
creativity = models.FloatField()
threat = models.FloatField()
ictindex = models.FloatField()
datetime = models.DateTimeField(default=timezone.now)
season = models.CharField(max_length=10)
# class Meta:
# unique_together = ('player','gwid','season') # Double gameweeks are lumped together
The above table had the unique_together combination that was commented out as a test to make this migration work. What can I do to make this migration work?