[Django] #32112: ./manage.py migrate generating invalid django_content_type inserts

13 views
Skip to first unread message

Django

unread,
Oct 16, 2020, 11:16:12 AM10/16/20
to django-...@googlegroups.com
#32112: ./manage.py migrate generating invalid django_content_type inserts
-----------------------------------------+------------------------
Reporter: jamercee | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 3.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
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).
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32112>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Oct 16, 2020, 12:34:15 PM10/16/20
to django-...@googlegroups.com
#32112: ./manage.py migrate generating invalid django_content_type inserts
-------------------------------------+-------------------------------------
Reporter: jamercee | Owner: nobody
Type: Uncategorized | Status: closed
Component: | Version: 3.1
contrib.contenttypes |
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => invalid
* component: Uncategorized => contrib.contenttypes


Comment:

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/32112#comment:1>

Django

unread,
Oct 16, 2020, 1:19:57 PM10/16/20
to django-...@googlegroups.com
#32112: ./manage.py migrate generating invalid django_content_type inserts
-------------------------------------+-------------------------------------
Reporter: jamercee | Owner: nobody

Type: Uncategorized | Status: closed
Component: | Version: 3.1
contrib.contenttypes |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by jamercee):

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

Thanks!

--
Ticket URL: <https://code.djangoproject.com/ticket/32112#comment:2>

Django

unread,
Mar 23, 2021, 8:36:25 AM3/23/21
to django-...@googlegroups.com
#32112: ./manage.py migrate generating invalid django_content_type inserts
-------------------------------------+-------------------------------------
Reporter: jamercee | Owner: nobody

Type: Uncategorized | Status: closed
Component: | Version: 3.1
contrib.contenttypes |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by flyte):

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),
]
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32112#comment:3>

Reply all
Reply to author
Forward
0 new messages