Disclaimers:
I have a model which looks like this:
class InputTypeMap(models.Model):
input_type = models.ForeignKey(InputType, on_delete=models.CASCADE)
training = models.ForeignKey(Training, on_delete=models.CASCADE)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
gender = models.ForeignKey(Gender, on_delete=models.CASCADE)
I.e. there are four fields in the model which are all foreign keys to other models. The primary key is not added explicitly by me, rather I trust Django to add the required primary key with an "invisible" AutoField. When I create a new instance of this class like:
InputTypeMap.objects.create(input_type=input_type,
training=training,
gender=gender,
category=category)
I get an exception:
Traceback (most recent call last):
File "/home/hove/sleipner/venv/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.NotNullViolation: null value in column "id" violates not-null constraint
DETAIL: Failing row contains (null, Maintenance, Female, MareGielding, No).
From the error message it seems to me that a ID key for the new entry is not generated; I was expecting postgres to do that itself? When the same code is run as a test on sqlite it works as intended.
Joakim