Disclaimers:

   1. This issue is also on StackOverflow:
   
https://stackoverflow.com/questions/64320386/django-postgresql-model-with-only-foreign-keys
   2. I posted this here in this group a couple of days ago without finding
   a solution.

I must admit I am quite baffled by this problem, seems like a thing I have
done for years suddenly does not work any longer?? In particular the code
in question here has worked for a couple of months, and then "suddenly"
stopped working - probably due to "apt get upgrade", but I can not pinpoint
exactly when it stopped working. I post it again here with some corrections
and a bit more detail. Hopefully someone can see what I do wrong; if
someone wants to contact me off-list and offer commercial help I could be
interested in that.


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.
Information about Postgres versions: Server 9.6 - Client 13

Joakim

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CALKD1M-wcpm9PGMHXrE-jBaPDxMy3R3MuGiSvwASe0re97%2Bj9w%40mail.gmail.com.

Reply via email to