﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
23455	migrations created with python2 break with python3	Brian May	Markus Holtermann	"As an example, my model has:

{{{
@python_2_unicode_compatible                                                     
class Institute(models.Model):
    ...
    delegates = models.ManyToManyField(                                          
        Person, related_name='delegate_for',                                     
        blank=True, null=True, through='InstituteDelegate')
    ...
}}}

makemigrations running under Python2 turns this into:

{{{
migrations.AddField(
    model_name='institute',
    name='delegates',
    field=models.ManyToManyField(related_name=b'delegate_for', null=True, through='karaage.InstituteDelegate', to='karaage.Person', blank=True),
    preserve_default=True,
),
}}}

This works fine under Python2, where `b''` == `''`.

Under Python3 however, I get:

{{{
Traceback (most recent call last):
  File ""./manage.py"", line 7, in <module>
    management.execute_from_command_line()
  File ""/usr/lib/python3/dist-packages/django/core/management/__init__.py"", line 385, in execute_from_command_line
    utility.execute()
  File ""/usr/lib/python3/dist-packages/django/core/management/__init__.py"", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File ""/usr/lib/python3/dist-packages/django/core/management/commands/test.py"", line 50, in run_from_argv
    super(Command, self).run_from_argv(argv)
  File ""/usr/lib/python3/dist-packages/django/core/management/base.py"", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File ""/usr/lib/python3/dist-packages/django/core/management/commands/test.py"", line 71, in execute
    super(Command, self).execute(*args, **options)
  File ""/usr/lib/python3/dist-packages/django/core/management/base.py"", line 338, in execute
    output = self.handle(*args, **options)
  File ""/usr/lib/python3/dist-packages/django/core/management/commands/test.py"", line 88, in handle
    failures = test_runner.run_tests(test_labels)
  File ""/usr/lib/python3/dist-packages/django/test/runner.py"", line 147, in run_tests
    old_config = self.setup_databases()
  File ""/usr/lib/python3/dist-packages/django/test/runner.py"", line 109, in setup_databases
    return setup_databases(self.verbosity, self.interactive, **kwargs)
  File ""/usr/lib/python3/dist-packages/django/test/runner.py"", line 299, in setup_databases
    serialize=connection.settings_dict.get(""TEST_SERIALIZE"", True),
  File ""/usr/lib/python3/dist-packages/django/db/backends/creation.py"", line 374, in create_test_db
    test_flush=True,
  File ""/usr/lib/python3/dist-packages/django/core/management/__init__.py"", line 115, in call_command
    return klass.execute(*args, **defaults)
  File ""/usr/lib/python3/dist-packages/django/core/management/base.py"", line 338, in execute
    output = self.handle(*args, **options)
  File ""/usr/lib/python3/dist-packages/django/core/management/commands/migrate.py"", line 160, in handle
    executor.migrate(targets, plan, fake=options.get(""fake"", False))
  File ""/usr/lib/python3/dist-packages/django/db/migrations/executor.py"", line 63, in migrate
    self.apply_migration(migration, fake=fake)
  File ""/usr/lib/python3/dist-packages/django/db/migrations/executor.py"", line 91, in apply_migration
    if self.detect_soft_applied(migration):
  File ""/usr/lib/python3/dist-packages/django/db/migrations/executor.py"", line 135, in detect_soft_applied
    apps = project_state.render()
  File ""/usr/lib/python3/dist-packages/django/db/migrations/state.py"", line 67, in render
    model.render(self.apps)
  File ""/usr/lib/python3/dist-packages/django/db/migrations/state.py"", line 311, in render
    body,
  File ""/usr/lib/python3/dist-packages/django/db/models/base.py"", line 171, in __new__
    new_class.add_to_class(obj_name, obj)
  File ""/usr/lib/python3/dist-packages/django/db/models/base.py"", line 300, in add_to_class
    value.contribute_to_class(cls, name)
  File ""/usr/lib/python3/dist-packages/django/db/models/fields/related.py"", line 2239, in contribute_to_class
    super(ManyToManyField, self).contribute_to_class(cls, name)
  File ""/usr/lib/python3/dist-packages/django/db/models/fields/related.py"", line 269, in contribute_to_class
    'app_label': cls._meta.app_label.lower()
TypeError: unsupported operand type(s) for %: 'bytes' and 'dict'
}}}

If I run makemigrations under Python 3, it does the correct thing:

{{{
migrations.AddField(
    model_name='institute',
    name='delegates',
    field=models.ManyToManyField(related_name='delegate_for', null=True, through='karaage.InstituteDelegate', to='karaage.Person', blank=True),
    preserve_default=True,
),
}}}

If I look at a diff, it seems that just about everywhere a string (in the above example, `through` and `to` appear to be ok) was used it is incorrectly quoted in the migration as a byte string.

Apologies if this is already reported or documented somewhere, I looked but couldn't see anything.

I have a vague recollection this may not have been a problem in the RC1 release, not absolutely sure now."	Bug	closed	Migrations	1.7	Release blocker	fixed		info+coding@…	Accepted	1	0	0	0	0	0
