Alan Tingey Alan Tingey - 3 months ago 10
MySQL Question

Exception Value: Cannot assign "x": "y" must be a "z" instance

I have a script to pull in football fixtures into my mySQL database. If I do not try to pull in the seasonid it works. But when I put the seasonid in it breaks and I am unsure why. Any help would be appreciated.

The models are as follows:

class StraightredSeason(models.Model):
seasonid = models.IntegerField(primary_key = True)
seasonyear = models.CharField(max_length = 4)
seasonname = models.CharField(max_length = 36)

def __unicode__(self):
return self.seasonid

class Meta:
managed = True
db_table = 'straightred_season'

class StraightredTeam(models.Model):
teamid = models.IntegerField(primary_key=True)
teamname = models.CharField(max_length=36)
country = models.CharField(max_length=36,null=True)
stadium = models.CharField(max_length=36,null=True)
homepageurl = models.TextField(null=True)
wikilink = models.TextField(null=True)
teamcode = models.CharField(max_length=5,null=True)
teamshortname = models.CharField(max_length=24,null=True)
currentteam = models.PositiveSmallIntegerField(null=True)

def __unicode__(self):
return self.teamname

class Meta:
managed = True
db_table = 'straightred_team'

class StraightredFixture(models.Model):
fixtureid = models.IntegerField(primary_key=True)
home_team = models.ForeignKey('straightred.StraightredTeam', db_column='hometeamid', related_name='home_fixtures')
away_team = models.ForeignKey('straightred.StraightredTeam', db_column='awayteamid', related_name='away_fixtures')
fixturedate = models.DateTimeField(null=True)
fixturestatus = models.CharField(max_length=24,null=True)
fixturematchday = models.IntegerField(null=True)
spectators = models.IntegerField(null=True)
hometeamscore = models.IntegerField(null=True)
awayteamscore = models.IntegerField(null=True)
homegoaldetails = models.TextField(null=True)
awaygoaldetails = models.TextField(null=True)
hometeamyellowcarddetails = models.TextField(null=True)
awayteamyellowcarddetails = models.TextField(null=True)
hometeamredcarddetails = models.TextField(null=True)
awayteamredcarddetails = models.TextField(null=True)
soccerseason = models.ForeignKey('straightred.StraightredSeason', db_column='soccerseasonid', related_name='fixture_season')


def __unicode__(self):
return self.fixtureid

class Meta:
managed = True
db_table = 'straightred_fixture'


The StraightredSeason table contains the following:

mysql> select * from straightred_season;
+----------+------------+------------+
| seasonid | seasonyear | seasonname |
+----------+------------+------------+
| 1025 | 16/1 | EPL |
+----------+------------+------------+


But whenever I run the code below I get the following error:

Cannot assign "1025": "StraightredFixture.soccerseason" must be a "StraightredSeason" instance.

fixtureUpdate = StraightredFixture(fixtureid=fixture['Id'],
away_team_id = fixture['AwayTeam_Id'],
home_team_id = fixture['HomeTeam_Id'],
fixturedate = fixture['Date'],
fixturestatus = fixture['Time'],
fixturematchday = fixture['Round'],
spectators = fixture['Spectators'],
hometeamscore = fixture['HomeGoals'],
awayteamscore = fixture['AwayGoals'],
homegoaldetails = fixture['HomeGoalDetails'],
awaygoaldetails = fixture['AwayGoalDetails'],
hometeamyellowcarddetails = fixture['HomeTeamYellowCardDetails'],
awayteamyellowcarddetails = fixture['AwayTeamYellowCardDetails'],
hometeamredcarddetails = fixture['HomeTeamRedCardDetails'],
awayteamredcarddetails = fixture['AwayTeamRedCardDetails'],
soccerseason = 1025)

Answer

You can't set a ForeignKey object as int. You can instead set the ForeignKey value (id associated with the FK object) by appending _id to the field:

fixtureUpdate = StraightredFixture(fixtureid=fixture['Id'],
                                   ...
                                   soccerseason_id = 1025)
#                                              ^^^
Comments