Performing migrations in Django - django.db.utils.IntegrityError: null values


I am adding the following attribute type choice to my model

class User(AbstractUser):
        (GENDER_MALE, u'male'),
        (GENDER_FEMALE, u'female'),
        (GENDER_OTHER, u'other'),
    gender = models.CharField(max_length=1, choices=GENDER_CHOICES, default=GENDER_OTHER,)

When I execute the migration, it tells me the following:

(nrb_dev)➜  project git:(master) ✗ python makemigrations
Migrations for 'userprofile':
    - Alter field gender on user
(nrb_dev)➜  project git:(master) ✗ python migrate 

Operations to perform:
  Apply all migrations: sessions, userprofile, auth, admin, contenttypes
Running migrations:
  Applying userprofile.0006_auto_20160116_2206...Traceback (most recent call last):
  File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/db/backends/", line 64, in execute
    return self.cursor.execute(sql, params)
psycopg2.IntegrityError: column "gender" contains null values

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "", line 11, in <module>
  File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/core/management/", line 350, in execute_from_command_line
  File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/core/management/", line 342, in execute
  File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/core/management/", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/core/management/", line 399, in execute
    output = self.handle(*args, **options)
  File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/core/management/commands/", line 200, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/db/migrations/", line 92, in migrate
    self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/db/migrations/", line 121, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/db/migrations/", line 198, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/db/migrations/", line 123, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/db/migrations/operations/", line 62, in database_forwards
  File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/db/backends/base/", line 396, in add_field
    self.execute(sql, params)
  File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/db/backends/base/", line 110, in execute
    cursor.execute(sql, params)
  File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/db/backends/", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/db/backends/", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/db/", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/utils/", line 685, in reraise
    raise value.with_traceback(tb)
  File "/home/bgarcial/.virtualenvs/nrb_dev/lib/python3.4/site-packages/django/db/backends/", line 64, in execute
    return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: column "gender" contains null values

(nrb_dev)➜  project git:(master) ✗

It tells me that the attribute or column gender that I am adding already contains a null value, but I do not understand if a choice type field from a tuple should be specified more when defining it.

I'm following the example as Django recommends but I do not know what my error.

The code of the migration that you are trying to apply is this:

# -*- coding: utf-8 -*-
# Generated by Django 1.9 on 2016-01-16 22:19
from __future__ import unicode_literals

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('userprofile', '0009_auto_20160116_2214'),

    operations = [
            field=models.CharField(choices=[('M', 'male'), ('F', 'female'), ('O', 'other')], default='O', max_length=1),

On the other hand, which is more recommendable, that in a form the user chooses the gender (male and female) using a select box or is it better an item type radio button?

asked by bgarcial 16.01.2016 в 23:35

1 answer


There is something very useful to take into account when establishing a migration and if, based on the data, migrations have been previously established, a false migration should be done as explained in the Django documentation

(nrb_dev)➜  neurorehabilitation-system git:(dev) ✗ python makemigrations

Migrations for 'userprofile':
    - Add field gender on user
(nrb_dev)➜ neurorehabilitation-system git:(master) ✗ python migrate --fake-initial 
Operations to perform: Apply all migrations: contenttypes, sessions, auth, userprofile, admin Running migrations: Applying userprofile.0006_user_gender... OK 
(nrb_dev)➜ neurorehabilitation-system git:(master) ✗

This way already in my database the migration has been applied adding the new field gender that was the one that wanted.

In addition to this, I found this post to be very useful where they talk about how to remove an application or model in Django, in the correct way so that the database will not be affected.

answered by 20.01.2016 / 16:22