Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#23752 closed New feature (wontfix)

Automate migrating a ForeignKey to ManyToManyField

Reported by: Adam Dobrawy Owned by: nobody
Component: Migrations Version: 1.7
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 (last modified by Adam Dobrawy)

I am unable to migrate ForeignKey to ManyToManyFields. I generated migrations by django-admin.py makemigratinons and I got error:

Applying bibliography.0013_auto_20141102_1346...Traceback (most recent call last):
  File "X/bin/django-admin.py", line 5, in <module>
    management.execute_from_command_line()
  File "X/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "X/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "X/local/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "X/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "X/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 160, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "X/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 63, in migrate
    self.apply_migration(migration, fake=fake)
  File "X/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 97, in apply_migration
    migration.apply(project_state, schema_editor)
  File "X/local/lib/python2.7/site-packages/django/db/migrations/migration.py", line 107, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File "X/local/lib/python2.7/site-packages/django/db/migrations/operations/fields.py", line 139, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "X/local/lib/python2.7/site-packages/django/db/backends/schema.py", line 470, in alter_field
    new_field,
ValueError: Cannot alter field bibliography.Publication.publisher into bibliography.Publication.publisher - they are not compatible types (you cannot alter to or from M2M fields, or add or remove through= on M2M fields)

I found workaround in few migrations, so...
.1. Create new fields ManyToManyFields in table (remember about related_name).

        migrations.AddField(
            model_name='publication',
            name='publisher_many',
            field=models.ManyToManyField(related_name='B', null=True, verbose_name='Publisher', to='bibliography.Publisher', blank=True),
            preserve_default=True,
        ),
        migrations.AddField(
            model_name='publication',
            name='publisher_org_many',
            field=models.ManyToManyField(related_name='B', null=True, verbose_name='NGO-publisher', to='org_bank.Organization', blank=True),
            preserve_default=True,
        ),

.2. Migrate data:

def move_to_many_fields(apps, schema_editor):
    Publication = apps.get_model("bibliography", "Publication")
    for obj in Publication.objects.filter(~Q(publisher__isnull=True) | ~Q(publisher_org__isnull=True)).all():
        obj.publisher_many = [obj.publisher]
        obj.publisher_org_many = [obj.publisher_org]
        obj.save()
…
        migrations.RunPython(move_to_many_fields),

.3. Remove old-fields

        migrations.RemoveField(
            model_name='publication',
            name='publisher',
        ),
        migrations.RemoveField(
            model_name='publication',
            name='publisher_org',
        ),

.4. Rename new fields to old-fields name

        migrations.RenameField(
            model_name='publication',
            old_name='publisher_many',
            new_name='publisher',
        ),
        migrations.RenameField(
            model_name='publication',
            old_name='publisher_org_many',
            new_name='publisher_org',
        ),

What do you think about integration this kind migrations to Django?

Change History (3)

comment:1 by Adam Dobrawy, 10 years ago

Description: modified (diff)

comment:2 by Tim Graham, 10 years ago

Resolution: wontfix
Status: newclosed

I'm not sure your solution would generalize to all situations. I think automatically generating a data migration is risky as well. If you wish to get other opinions, please raise the issue on the DevelopersMailingList. Thanks!

comment:3 by Tim Graham, 10 years ago

Summary: Migrating ForeignKey to ManyToManyField raise ValueError("…they are not compatible types…")Automate migrating a ForeignKey to ManyToManyField
Note: See TracTickets for help on using tickets.
Back to Top