[Django] #32893: Missing import statement in generated migration (NameError: name 'models' is not defined)

52 views
Skip to first unread message

Django

unread,
Jun 30, 2021, 3:19:51 PM6/30/21
to django-...@googlegroups.com
#32893: Missing import statement in generated migration (NameError: name 'models'
is not defined)
-------------------------------------+-------------------------------------
Reporter: JaapJoris | Owner: nobody
Type: Bug | Status: new
Component: | Version: 3.2
Migrations | Keywords: migrations writer
Severity: Normal | missing import
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-------------------------------------+-------------------------------------
I found a bug in Django's latest release: 3.2.4.

Given the following contents of `models.py`:

{{{
from django.db import models

class MyField(models.TextField):
pass

class MyBaseModel(models.Model):
class Meta:
abstract = True

class MyMixin:
pass

class MyModel(MyMixin, MyBaseModel):
name = MyField(primary_key=True)
}}}

The `makemigrations` command will generate the following migration file:

{{{
# Generated by Django 3.2.4 on 2021-06-30 19:13

import app.models
from django.db import migrations


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='MyModel',
fields=[
('name', app.models.MyField(primary_key=True,
serialize=False)),
],
options={
'abstract': False,
},
bases=(app.models.MyMixin, models.Model),
),
]
}}}

Which will then fail with the following error:

{{{
File "/home/jj/django_example/app/migrations/0001_initial.py", line 7,
in <module>
class Migration(migrations.Migration):
File "/home/jj/django_example/app/migrations/0001_initial.py", line 23,
in Migration
bases=(app.models.MyMixin, models.Model),
NameError: name 'models' is not defined
}}}

Expected behavior: Django generates a migration file that is valid Python.

Actual behavior: Django generates a migration file that is missing an
import statement.

I think this is a bug of the module `django.db.migrations.writer`, but I'm
not sure. I will be happy to assist with debugging.

Thanks for your attention,
Jaap Joris

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

Django

unread,
Jun 30, 2021, 11:17:52 PM6/30/21
to django-...@googlegroups.com
#32893: Missing import statement in generated migration (NameError: name 'models'
is not defined)
-------------------------------------+-------------------------------------
Reporter: Jaap Joris Vens | Owner: Abhyudai
Type: Bug | Status: assigned
Component: Migrations | Version: dev
Severity: Normal | Resolution:
Keywords: migrations writer | Triage Stage: Accepted
missing import |
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Abhyudai):

* owner: nobody => Abhyudai
* status: new => assigned
* version: 3.2 => dev
* easy: 1 => 0
* stage: Unreviewed => Accepted


Comment:

I could reproduce the issue with `3.2.4`, `2.2.24` and the `main` branch.
For what it's worth, the issue doesn't occur if the class `MyModel` does
inherit from `MyMixin`.

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

Django

unread,
Jul 1, 2021, 12:43:59 AM7/1/21
to django-...@googlegroups.com
#32893: Missing import statement in generated migration (NameError: name 'models'
is not defined)
-------------------------------------+-------------------------------------
Reporter: Jaap Joris Vens | Owner: Abhyudai
Type: Bug | Status: assigned
Component: Migrations | Version: dev
Severity: Normal | Resolution:
Keywords: migrations writer | Triage Stage: Accepted
missing import |
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak):

`MyBaseModel` is not necessary to reproduce this issue, it's due to the
fact that `MyModel` doesn't have fields from `django.db.models` and has
custom `bases`. It looks like an issue with special casing of
`models.Model` in `TypeSerializer`. Proposed patch

{{{
diff --git a/django/db/migrations/serializer.py
b/django/db/migrations/serializer.py
index e19c881cda..6e78462e95 100644
--- a/django/db/migrations/serializer.py
+++ b/django/db/migrations/serializer.py
@@ -273,7 +273,7 @@ class TupleSerializer(BaseSequenceSerializer):
class TypeSerializer(BaseSerializer):
def serialize(self):
special_cases = [
- (models.Model, "models.Model", []),
+ (models.Model, "models.Model", ['from django.db import
models']),
(type(None), 'type(None)', []),
]
for case, string, imports in special_cases:
}}}

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

Django

unread,
Jul 1, 2021, 3:40:07 AM7/1/21
to django-...@googlegroups.com
#32893: Missing import statement in generated migration (NameError: name 'models'
is not defined)
-------------------------------------+-------------------------------------
Reporter: Jaap Joris Vens | Owner: Abhyudai
Type: Bug | Status: assigned
Component: Migrations | Version: dev
Severity: Normal | Resolution:
Keywords: migrations writer | Triage Stage: Accepted
missing import |
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Abhyudai):

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/14580/ pull request]

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

Django

unread,
Jul 1, 2021, 5:51:54 AM7/1/21
to django-...@googlegroups.com
#32893: Missing import statement in generated migration (NameError: name 'models'
is not defined)
-------------------------------------+-------------------------------------
Reporter: Jaap Joris Vens | Owner: Abhyudai
Type: Bug | Status: assigned
Component: Migrations | Version: dev
Severity: Normal | Resolution:
Keywords: migrations writer | Triage Stage: Ready for
missing import | checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/32893#comment:4>

Django

unread,
Jul 1, 2021, 7:30:55 AM7/1/21
to django-...@googlegroups.com
#32893: Missing import statement in generated migration (NameError: name 'models'
is not defined)
-------------------------------------+-------------------------------------
Reporter: Jaap Joris Vens | Owner: Abhyudai
Type: Bug | Status: closed
Component: Migrations | Version: dev
Severity: Normal | Resolution: fixed

Keywords: migrations writer | Triage Stage: Ready for
missing import | checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"fde6fb28984a76d5bee794f4c0458eb25fe56fcd" fde6fb2]:
{{{
#!CommitTicketReference repository=""
revision="fde6fb28984a76d5bee794f4c0458eb25fe56fcd"
Fixed #32893 -- Fixed serialization of models.Model class in migrations.

Migrations assumed that an import of the models.Model class must
already be included when it's serialized, but for models with only
custom fields this was not necessarily the case.

Thanks Jaap Joris Vens for the report.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32893#comment:5>

Reply all
Reply to author
Forward
0 new messages