Skip to content
Advertisement

IntegrityError NOT NULL constraint failed: – in validating form missing user id (foreing key of anothermodel)

Previous title: Unable to import ‘misaka’ – Django

I am following a tutorial about how to build a social network.

I run into this error:

IntegrityError at /posts/new/ NOT NULL constraint failed: posts_post.user_id

Complete traceback (please note the lines in bold):

JavaScript

The lines in bold are pointing to these two pieces of code:

1: In my_projectpostsviews.py in form_valid:

JavaScript

2: In my_projectpostsmodels.py in save

JavaScript

These functions are run when a user posts a new post inside a group of the social network.

I am using VScode, so I noticed that apparently, my code does not recognise misaka, installed via pip and imported as follows:

JavaScript

Pylint import error: Unable to import ‘misaka’

I have read misaka documentation but I can’t understand what is wrong.
please note I am using Misaka v. 2.1.0, that apparently does not need to be installed in INSTALLED_APPS in settings.

Any thoughts?

Since it has been suggested that I am trying to create a Post model object into the database without setting the user foreign key, here you can see my my_project/posts/model.py:

JavaScript

Advertisement

Answer

You’re trying to insert a Post model object into the database without setting the User foreign key.

You should set it to the user object attached to the request first.

JavaScript

Check Django Documentation

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement