Opened 5 years ago

Closed 5 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


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 "./", line 10, in <module>
  File "/home/jstaab/.virtualenvs/m2mcasesensitivity/local/lib/python2.7/site-packages/django/core/management/", line 338, in execute_from_command_line
  File "/home/jstaab/.virtualenvs/m2mcasesensitivity/local/lib/python2.7/site-packages/django/core/management/", line 330, in execute
  File "/home/jstaab/.virtualenvs/m2mcasesensitivity/local/lib/python2.7/site-packages/django/core/management/", 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/", line 444, in execute
    output = self.handle(*args, **options)
  File "/home/jstaab/.virtualenvs/m2mcasesensitivity/local/lib/python2.7/site-packages/django/core/management/commands/", 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/", 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/", 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/", 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/", 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/", 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/", line 232, in _alter_many_to_many
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
./ startapp m2mtestcase
sleep 1 # Race condition?
sed -i 's/INSTALLED_APPS = (/INSTALLED_APPS = ("m2mtestcase",/' m2mcasesensitivity/
echo "
from django.db import models

class FirstProblemModel(models.Model): 

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

Change History (2)

comment:1 Changed 5 years ago by Tim Graham

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

comment:2 Changed 5 years ago by Tim Graham

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