Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#32112 closed Uncategorized (invalid)

./manage.py migrate generating invalid django_content_type inserts

Reported by: jamercee Owned by: nobody
Component: contrib.contenttypes Version: 3.1
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

We've been working on a django project and just run into a problem with the django ./manage.py migrate command raising django.db.utils.IntegrityError during post-migration.

The IntegrityError is caused by the migrate command attempting to insert a new record into the django_content_type table. It is providing the app_label and model column values, but not providing value for the name column which violates the non-null constraint.

What's interesting is that ./manage.py test works just fine.

Here's the full stack trace.

Running migrations:
  No migrations to apply.
Running post-migrate handlers for application utils
Traceback (most recent call last):
  File "/home/jim/src/dc/.pyenv/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.NotNullViolation: null value in column "name" violates not-null constraint
DETAIL:  Failing row contains (46, null, utils, testutilsguidfield).


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

  File "./manage.py", line 22, in <module>
    main()
  File "./manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/home/jim/src/dc/.pyenv/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/home/jim/src/dc/.pyenv/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/jim/src/dc/.pyenv/lib/python3.8/site-packages/django/core/management/base.py", line 330, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/jim/src/dc/.pyenv/lib/python3.8/site-packages/django/core/management/base.py", line 371, in execute
    output = self.handle(*args, **options)
  File "/home/jim/src/dc/.pyenv/lib/python3.8/site-packages/django/core/management/base.py", line 85, in wrapped
    res = handle_func(*args, **kwargs)
  File "/home/jim/src/dc/.pyenv/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 267, in handle
    emit_post_migrate_signal(
  File "/home/jim/src/dc/.pyenv/lib/python3.8/site-packages/django/core/management/sql.py", line 48, in emit_post_migrate_signal
    models.signals.post_migrate.send(
  File "/home/jim/src/dc/.pyenv/lib/python3.8/site-packages/django/dispatch/dispatcher.py", line 177, in send
    return [
  File "/home/jim/src/dc/.pyenv/lib/python3.8/site-packages/django/dispatch/dispatcher.py", line 178, in <listcomp>
    (receiver, receiver(signal=self, sender=sender, **named))
  File "/home/jim/src/dc/.pyenv/lib/python3.8/site-packages/django/contrib/auth/management/__init__.py", line 42, in create_permissions
    create_contenttypes(app_config, verbosity=verbosity, interactive=interactive, using=using, apps=apps, **kwargs)
  File "/home/jim/src/dc/.pyenv/lib/python3.8/site-packages/django/contrib/contenttypes/management/__init__.py", line 132, in create_contenttypes
    ContentType.objects.using(using).bulk_create(cts)
  File "/home/jim/src/dc/.pyenv/lib/python3.8/site-packages/django/db/models/query.py", line 506, in bulk_create
    returned_columns = self._batched_insert(
  File "/home/jim/src/dc/.pyenv/lib/python3.8/site-packages/django/db/models/query.py", line 1266, in _batched_insert
    inserted_rows.extend(self._insert(
  File "/home/jim/src/dc/.pyenv/lib/python3.8/site-packages/django/db/models/query.py", line 1249, in _insert
    return query.get_compiler(using=using).execute_sql(returning_fields)
  File "/home/jim/src/dc/.pyenv/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1395, in execute_sql
    cursor.execute(sql, params)
  File "/home/jim/src/dc/.pyenv/lib/python3.8/site-packages/django/db/backends/utils.py", line 98, in execute
    return super().execute(sql, params)
  File "/home/jim/src/dc/.pyenv/lib/python3.8/site-packages/django/db/backends/utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/jim/src/dc/.pyenv/lib/python3.8/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/jim/src/dc/.pyenv/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/jim/src/dc/.pyenv/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/jim/src/dc/.pyenv/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: null value in column "name" violates not-null constraint
DETAIL:  Failing row contains (46, null, utils, testutilsguidfield).

Change History (3)

comment:1 by Mariusz Felisiak, 4 years ago

Component: Uncategorizedcontrib.contenttypes
Resolution: invalid
Status: newclosed

I'm not sure which version of Django are you using, because ContentType.name was removed in Django 1.8, see #24099. Django < 2.2 is not supported anymore.

comment:2 by jamercee, 4 years ago

That's it. We're migrating a django project from 1.6 to 3.0 and didn't know about the deprecation.

Thanks!

in reply to:  description comment:3 by Ellis Percival, 4 years ago

I had this problem on Django 3.1.7 because the contenttypes migration 0002_remove_content_type_name was not being run before my migration.

Fixed it by adding it as a dependency:

from django.apps import apps
from django.contrib.auth.management import create_permissions
from django.db import migrations


def create_all_permissions(*args, **kwargs):
    for app_config in apps.get_app_configs():
        app_config.models_module = True
        create_permissions(app_config, apps=apps, verbosity=0)
        app_config.models_module = None


def create_moderation_group(apps, schema_editor):
    Permission = apps.get_model("auth", "Permission")
    perms = Permission.objects.filter(
        codename__in=["moderate_submission", "view_submission"]
    )
    assert len(perms) == 2
    Group = apps.get_model("auth", "Group")
    group = Group.objects.create(name="Submission Moderators")
    group.permissions.set(perms)


class Migration(migrations.Migration):

    dependencies = [
        ("api", "0001_initial"),
        ("contenttypes", "0002_remove_content_type_name"),
    ]

    operations = [
        migrations.RunPython(create_all_permissions),
        migrations.RunPython(create_moderation_group),
    ]
Note: See TracTickets for help on using tickets.
Back to Top