Opened 9 months ago

Closed 9 months ago

Last modified 9 months ago

#35199 closed Bug (invalid)

Makemigrations raises KeyError when removing a field from Meta.unique_together when its a list

Reported by: Paco Martínez Owned by: nobody
Component: Migrations Version: 4.2
Severity: Normal Keywords: migrations, unique_together, makemigrations
Cc: Paco Martínez Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Paco Martínez)

Having a model like this, with a unique_together with a list of lists:

from django.db import models

class MyModel(models.Model):
    field1 = models.CharField(max_length=50)
    field2 = models.ForeignKey()
    field3 = models.ForeignKey()

    class Meta:
        unique_together=[['field1', 'field2', 'field3']]

If we want to remove one of the fields from the constraint like:

    class Meta:
        unique_together=[['field1', 'field2']]

When we run the makemigrations command, it raises a KeyError. This is the traceback:

Traceback (most recent call last):
  File "/code/manage.py", line 26, in <module>
    main()
  File "/code/manage.py", line 22, in main
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.12/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.12/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.12/site-packages/django/core/management/base.py", line 412, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.12/site-packages/django/core/management/base.py", line 458, in execute
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/core/management/base.py", line 106, in wrapper
    res = handle_func(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/core/management/commands/makemigrations.py", line 233, in handle
    changes = autodetector.changes(
              ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/db/migrations/autodetector.py", line 46, in changes
    changes = self._detect_changes(convert_apps, graph)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/db/migrations/autodetector.py", line 192, in _detect_changes
    self.generate_removed_altered_unique_together()
  File "/usr/local/lib/python3.12/site-packages/django/db/migrations/autodetector.py", line 1506, in generate_removed_altered_unique_together
    self._generate_removed_altered_foo_together(operations.AlterUniqueTogether)
  File "/usr/local/lib/python3.12/site-packages/django/db/migrations/autodetector.py", line 1481, in _generate_removed_altered_foo_together
    for (
  File "/usr/local/lib/python3.12/site-packages/django/db/migrations/autodetector.py", line 1462, in _get_altered_foo_together_operations
    field = new_model_state.get_field(field_name)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/db/migrations/state.py", line 765, in get_field
    return self.fields[field_name]
           ~~~~~~~~~~~^^^^^^^^^^^^
KeyError: 'field1'

This does NOT happen when the unique_together option is a tuple. It works as expected creating the migration.

Change History (1)

comment:1 by Paco Martínez, 9 months ago

Description: modified (diff)
Resolution: invalid
Status: newclosed

Sorry but trying a bit more I discovered that the fields in the unique_together were named with the "_id" suffix as they are ForeignKey. Don't know how the constraint was there for several months and working fine. Now it's enforcing to name the fields without the suffix.

With that the makemigrations command runs as expected.

Sorry for the inconvenience of creating the ticket.

Last edited 9 months ago by Paco Martínez (previous) (diff)
Note: See TracTickets for help on using tickets.
Back to Top