Opened 8 years ago

Closed 8 years ago

#28080 closed Bug (duplicate)

Migration guesses wrong namespace for Enum defined on class

Reported by: Curtis Maloney Owned by: nobody
Component: Migrations Version: 1.11
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

I have the following model:

class Question(models.Model):
    @enum.unique
    class MODE(enum.IntEnum):
        TEXT = 1
        NUMBER = 2
        CHOICE = 3
 
    question = models.TextField()
    label_text = models.CharField(max_length=200)
    hint = models.CharField(max_length=200, blank=True)
    mode = models.IntegerField(
        default=MODE.TEXT,
        choices=((x.value, x.name.title()) for x in MODE),
    )

The initial migration generated produces:

        migrations.CreateModel(
            name='Question',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('question', models.TextField()),
                ('label_text', models.CharField(max_length=200)),
                ('hint', models.CharField(blank=True, max_length=200)),
                ('mode', models.IntegerField(choices=[(1, 'Text'), (2, 'Number'), (3, 'Choice')], default=sheets.models.MODE(1))),
            ],
        ),

Notice the default... it's sheets.models.MODE instead of sheets.models.Question.MODE

This causes the following traceback:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File ".../venv/lib/python3.5/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File ".../venv/lib/python3.5/site-packages/django/core/management/__init__.py", line 355, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File ".../venv/lib/python3.5/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File ".../venv/lib/python3.5/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File ".../venv/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 83, in handle
    executor = MigrationExecutor(connection, self.migration_progress_callback)
  File ".../venv/lib/python3.5/site-packages/django/db/migrations/executor.py", line 20, in __init__
    self.loader = MigrationLoader(self.connection)
  File ".../venv/lib/python3.5/site-packages/django/db/migrations/loader.py", line 52, in __init__
    self.build_graph()
  File ".../venv/lib/python3.5/site-packages/django/db/migrations/loader.py", line 203, in build_graph
    self.load_disk()
  File ".../venv/lib/python3.5/site-packages/django/db/migrations/loader.py", line 114, in load_disk
    migration_module = import_module("%s.%s" % (module_name, migration_name))
  File ".../venv/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 673, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File ".../cloudselect/sheets/migrations/0001_initial.py", line 10, in <module>
    class Migration(migrations.Migration):
  File ".../cloudselect/sheets/migrations/0001_initial.py", line 25, in Migration
    ('mode', models.IntegerField(choices=[(1, 'Text'), (2, 'Number'), (3, 'Choice')], default=sheets.models.MODE(1))),
AttributeError: module 'sheets.models' has no attribute 'MODE'

Change History (1)

comment:1 by Simon Charette, 8 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #27914.

Note: See TracTickets for help on using tickets.
Back to Top