Opened 9 years ago

Closed 9 years ago

#25429 closed Bug (duplicate)

M2M migration is inconsistently case-sensitive

Reported by: Jon Staab Owned by: nobody
Component: Migrations Version: 1.8
Severity: Normal Keywords: case sensitivity migrations manytomany
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When I rename a model and its uses to the same name with letters in different cases (e.g., "TestThing" > "Testthing") in ManyToManyForeign keys pointing to it, I get odd behavior in migrations. At times, the migration gets stuck, and tries to run the same change multiple times (since the relevant information is lowercase, it can't tell the difference between the old state and the new state), and other times, I get an error similar to the following:

Applying m2mtestcase.0003_auto_20150918_1747...Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/jstaab/.virtualenvs/m2mcasesensitivity/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/home/jstaab/.virtualenvs/m2mcasesensitivity/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/jstaab/.virtualenvs/m2mcasesensitivity/local/lib/python2.7/site-packages/django/core/management/base.py", line 393, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/jstaab/.virtualenvs/m2mcasesensitivity/local/lib/python2.7/site-packages/django/core/management/base.py", line 444, in execute
    output = self.handle(*args, **options)
  File "/home/jstaab/.virtualenvs/m2mcasesensitivity/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 222, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/home/jstaab/.virtualenvs/m2mcasesensitivity/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 110, in migrate
    self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
  File "/home/jstaab/.virtualenvs/m2mcasesensitivity/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 148, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/jstaab/.virtualenvs/m2mcasesensitivity/local/lib/python2.7/site-packages/django/db/migrations/migration.py", line 115, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/home/jstaab/.virtualenvs/m2mcasesensitivity/local/lib/python2.7/site-packages/django/db/migrations/operations/fields.py", line 201, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/home/jstaab/.virtualenvs/m2mcasesensitivity/local/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 469, in alter_field
    return self._alter_many_to_many(model, old_field, new_field, strict)
  File "/home/jstaab/.virtualenvs/m2mcasesensitivity/local/lib/python2.7/site-packages/django/db/backends/sqlite3/schema.py", line 232, in _alter_many_to_many
    old_field.rel.through._meta.get_field(old_field.m2m_reverse_field_name()),
AttributeError: 'ManyToManyField' object has no attribute 'm2m_reverse_field_name'

Here's a shell script that you can run to create a django app and run migrations that will cause this error (it assumes you're using virtualenvwrapper and have access to GNU sed. Tweak it as necessary):

mkvirtualenv m2mcasesensitivity
pip install Django===1.8.4
django-admin startproject m2mcasesensitivity
cd m2mcasesensitivity
./manage.py startapp m2mtestcase
sleep 1 # Race condition?
sed -i 's/INSTALLED_APPS = (/INSTALLED_APPS = ("m2mtestcase",/' m2mcasesensitivity/settings.py
echo "
from django.db import models

class FirstProblemModel(models.Model): 
    pass 


class SecondProblemModel(models.Model): 
    first_things = models.ManyToManyField(FirstProblemModel) 
" > m2mtestcase/models.py
./manage.py makemigrations 
./manage.py migrate
sed -i 's/ProblemModel/problemmodel/g' m2mtestcase/models.py
./manage.py makemigrations
./manage.py migrate
sed -i 's/problemmodel/ProblemModel/g' m2mtestcase/models.py
./manage.py makemigrations
./manage.py migrate
To tear down:
deactivate 
rmvirtualenv m2mcasesensitivity
cd ..
rm -rf m2mcasesensitivity

Change History (2)

comment:1 by Tim Graham, 9 years ago

Seems to be similar to #23916. Do you think there's any reason to keep two tickets open?

comment:2 by Tim Graham, 9 years ago

Resolution: duplicate
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top