Why am I getting a IntegrityError at, null value in a column that doesn't exists… Django

Question:

I am trying to hit an external api, when a user submits a form.
I am using Django and Postgresql

My Model

class League_Mod(models.Model):
    host = models.CharField(max_length=50)
    Espn_League_Id = models.IntegerField(unique = True)
    Espn_S2 = models.CharField(max_length=3000)
    Espn_Swid = models.CharField(max_length=300) 
    bigdata = models.JSONField(default=dict,null=True)

My Serializer

    class Meta:
        model = League_Mod
        fields = ['host', 'Espn_League_Id','Espn_S2','Espn_Swid','bigdata']

Views

where Owners is a large dictionary.

league_data = {
            'host' : request.data['host'],
            'Espn_League_Id' :request.data['Espn_League_Id'],
            'Espn_S2' : request.data['Espn_S2'],
            'Espn_Swid' : request.data['Espn_Swid'],
            'bigdata' : Owners
  }
serializer = LeagueSerializer(data=league_data)
print(serializer)
if serializer.is_valid(raise_exception=True):
    serializer.save()
    return  Response(serializer.data)

my print serializer runs, and prints the data correctly.

But I get an error:

integrityError at /wel/
null value in column "hello" of relation "api_league_mod" violates not-null constraint
DETAIL:  Failing row contains (11, JPFL, 216415, AEAylLD7uSQQ7%2BenPr6av1H%2Fx0Hqbbpn8Jvr91ngxM1ll5ynO685mhN%2BSu..., {D19D67CA-C981-4CA2-8463-AF4111D2E8E2}, {"Person1": {"2010": [0, 1, 2, 2, 2, 3, 3, 3, 3, 4, 5, 5, 6,..., null).

I am very unclear where the column hello is… and there is no relation to api_league_mod Model, so don’t quite understand why my serializer is returning unvalid

Any insight would be appreciated. Thanks!

Asked By: Jordan Freundlich

||

Answers:

The column hello is probably present in the database, which means it was in your League_Mod model and was removed later. This removal however is not reflected in any of your migrations.

You could try to run manage.py makemigrations and check the output, it might create the delete operation for the hello field.

Then run manage.py migrate to apply the changes to the DB

Answered By: Simone Pozzoli