How to Solve Error Message You are trying to add a non-nullable field ‘user’ to employee without a default; in Django Application

Posted on

Introduction

When there is an execution for creating a migration script, there is an error appear. The error itself appear after modifying the model which is using the MPTTModel. Actually, MPTTModel is a model exist in a specific module or library. It is a library acts as an utilities for implementing modified Preorder Tree Traversal with the available models and working with trees of the model instances. But after modifying that model, the error as exist in the title of this article appear.

(env) C:\programming\python\django\myproject>python manage.py makemigrations
You are trying to add a non-nullable field 'user' to employee without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows with a null value for this column)
2) Quit, and let me add a default in models.py
Select an option: 1
Please enter the default value now, as valid Python
The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now
Type 'exit' to exit this prompt
>>> exit

Actually, the following is the actual modification of the model :

from django.contrib.auth.models import User

class Employee(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)

Solution

So, there is an output forcing to set the user associated to the employee model. That is because there are already records existing of the employee model in its associated table. Another table, in this context it is the user table. That table has a link or connection to the employee table. But the problem is that by default the user will not have any value at all because of the migration process. So, the command ‘python manage.py makemigrations’ is forcing to set the value for each user of the employee. How can this thing be avoided without having to set the value for the user at all ?. Actually, the solution is very easy. Just add another parameter in the definition of the user to allow null value as follows :

from django.contrib.auth.models import User

class Employee(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, blank=true, null=True)

After modifying it as exist in the above definition, just execute the command once more :

(env) C:\programming\python\django\myproject>python manage.py makemigrations
Migrations for 'org':
org\migrations\0010_employee_user.py
- Add field user to employee

(env) C:\programming\python\django\myproject>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

You have 1 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): org.
Run 'python manage.py migrate' to apply them.
October 21, 2021 - 10:23:10
Django version 3.2.6, using settings 'core.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

(env) C:\programming\python\django\myproject>python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, org, sessions
Running migrations:
Applying org.0010_employeee_user... OK

(env) C:\programming\python\django\myproject>python manage.py migrate

Leave a Reply