#23752: Migrating ForeignKey to ManyToManyField raise ValueError("…they are not
compatible types…")
-----------------------------+--------------------------------------
Reporter: ad-m | Owner: nobody
Type: New feature | Status: new
Component: Migrations | Version: 1.7
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+--------------------------------------
Changes (by ad-m):
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
Old description:
> 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?
New description:
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?
--
--
Ticket URL: <https://code.djangoproject.com/ticket/23752#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/062.fef730d6096c13600ae4e61ca9ecba81%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.