Changes between Initial Version and Version 1 of Ticket #23752
- Timestamp:
- Nov 2, 2014, 4:55:29 PM (10 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Ticket #23752 – Description
initial v1 1 1 I am unable to migrate ForeignKey to ManyToManyFields. I generated migrations by ```django-admin.py makemigratinons` and I got error: 2 ``` 2 3 {{{ 3 4 Applying bibliography.0013_auto_20141102_1346...Traceback (most recent call last): 4 5 File "X/bin/django-admin.py", line 5, in <module> … … 25 26 new_field, 26 27 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) 27 ``` 28 29 }}} 28 30 29 31 I found workaround in few migrations, so... 30 32 .1. Create new fields ManyToManyFields in table (remember about related_name). 31 ``` 33 34 {{{ 32 35 migrations.AddField( 33 36 model_name='publication', … … 42 45 preserve_default=True, 43 46 ), 44 ``` 47 48 }}} 45 49 .2. Migrate data: 46 ``` 50 51 {{{ 47 52 def move_to_many_fields(apps, schema_editor): 48 53 Publication = apps.get_model("bibliography", "Publication") … … 53 58 … 54 59 migrations.RunPython(move_to_many_fields), 55 ``` 60 61 }}} 56 62 .3. Remove old-fields 57 ``` 63 64 {{{ 58 65 migrations.RemoveField( 59 66 model_name='publication', … … 64 71 name='publisher_org', 65 72 ), 66 ``` 73 }}} 67 74 .4. Rename new fields to old-fields name 68 ``` 75 76 {{{ 69 77 migrations.RenameField( 70 78 model_name='publication', … … 77 85 new_name='publisher_org', 78 86 ), 79 ``` 87 88 }}} 80 89 81 90 What do you think about integration this kind migrations to Django?