#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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/052.267e9c85381c3e142255a6966170eecd%40djangoproject.com.

Reply via email to