Changes between Initial Version and Version 1 of Ticket #23752


Ignore:
Timestamp:
Nov 2, 2014, 4:55:29 PM (10 years ago)
Author:
Adam Dobrawy
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #23752 – Description

    initial v1  
    11I am unable to migrate ForeignKey to ManyToManyFields. I generated migrations by ```django-admin.py makemigratinons` and I got error:
    2 ```
     2
     3{{{
    34Applying bibliography.0013_auto_20141102_1346...Traceback (most recent call last):
    45  File "X/bin/django-admin.py", line 5, in <module>
     
    2526    new_field,
    2627ValueError: 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)
    27 ```
     28
     29}}}
    2830
    2931I found workaround in few migrations, so...
    3032.1. Create new fields ManyToManyFields in table (remember about related_name).
    31 ```
     33
     34{{{
    3235        migrations.AddField(
    3336            model_name='publication',
     
    4245            preserve_default=True,
    4346        ),
    44 ```
     47
     48}}}
    4549.2. Migrate data:
    46 ```
     50
     51{{{
    4752def move_to_many_fields(apps, schema_editor):
    4853    Publication = apps.get_model("bibliography", "Publication")
     
    5358
    5459        migrations.RunPython(move_to_many_fields),
    55 ```
     60
     61}}}
    5662.3. Remove old-fields   
    57 ```
     63
     64{{{
    5865        migrations.RemoveField(
    5966            model_name='publication',
     
    6471            name='publisher_org',
    6572        ),
    66 ```
     73}}}
    6774.4. Rename new fields to old-fields name
    68 ```
     75
     76{{{
    6977        migrations.RenameField(
    7078            model_name='publication',
     
    7785            new_name='publisher_org',
    7886        ),
    79 ```
     87
     88}}}
    8089
    8190What do you think about integration this kind migrations to Django?
Back to Top