11👍
You have to make sure that the model you are creating the ‘ManyToManyField’ is already created in the database.
You can do that by adding as a dependency the migration where the model is created to your migration in which you alter the field:
Scenario 1: You alter the field to ‘ManyToManyField’ with a model from other app
class Migration(migrations.Migration):
dependencies = [
..........
('[app]', '__first__'),
]
operations = [
.........
]
Scenario 2: You create a ‘ManyToManyField’ and the model you are referring to is in the same file:
class Migration(migrations.Migration):
dependencies = [
..........
]
operations = [
.........
# Make sure the model you are making the reference with is before the ManyToManyField
migrations.CreateModel(...) ,
migrations.AlterField/CreateField(...)
]
7👍
I ran into the same problem, but I don’t know if for the same reasons. Luckily I don’t have any important data in the system, so I just changed the migration as follows – but note that this deletes all data in these columns!
Before:
operations = [
migrations.AlterField(
model_name='resource',
name='authors',
field=models.ManyToManyField(related_name='resources_authored', to='api.Person'),
),
migrations.AlterField(
model_name='resource',
name='editors',
field=models.ManyToManyField(blank=True, related_name='resources_edited', to='api.Person'),
),
]
After:
operations = [
migrations.RemoveField(
model_name='resource',
name='authors',
),
migrations.RemoveField(
model_name='resource',
name='editors',
),
migrations.AddField(
model_name='resource',
name='authors',
field=models.ManyToManyField(related_name='resources_authored', to='api.Person'),
),
migrations.AddField(
model_name='resource',
name='editors',
field=models.ManyToManyField(blank=True, related_name='resources_edited', to='api.Person'),
),
]
While the altering failed for mysterious reasons, removing and recreating the fields worked.
2👍
One of the reason might be your api.Person
model migrations might not have run before the one in which it is referenced(in your example it is file_manager.0006_auto_20160109_1536
). So make sure api.Person
migrations are run before, by adding them in the dependencies of file_manager.0006_auto_20160109_1536
.
- Use imaplib and oauth for connection with Gmail
- Iterating through model fields – Django
- Django on Google App Engine
- How to add custom page to django admin with custom form, not related to any Model?
0👍
I had the same problem when I tried to rename a table which was referenced with a many to many fields.
I resolved it this way:
– dumped the manytomany relationship data into a file
– removed the manytomany field and migrated
– renamed the table and migrated
– added the manytomany field back and migrated and loaded the relationships from the dump
- Using cleaned_data on forms in django
- Efficient function to retrieve a queryset of ancestors of an mptt queryset
- Signing in leads to /accounts/profile/ in Django
- How to test django caching?
0👍
I also had the same problem, but the way that I fixed it, is the following steps:
Nota: You will lose data from that ManyToManyField
python manage.py makemigrations app_name
python manage.py migrate app_name --fake
Don’t forget the –fake
After that.
I removed (or just comment the line) the ManyToMany field, and then makemigrations app_name, migrate app_name.
At this step you have this column from your database cleared, what you need to do now is to re add the ManyToManyField, so when you makemigrations app_name & migrate app_name again.
You server can run perfectly
It’s not the best, but it worked for me.
hope it will help !
- How to export Django model data into CSV file
- Makemessages for an app installed in virtualenv
- What's the most efficient way to insert thousands of records into a table (MySQL, Python, Django)
- Use slugify in template
0👍
My problem came with changing a field from a choices field to a manytomanyfield.
If you do this and keep the same name, then you’ll have issues.
I followed Denis Drescher:
Basically go through your migration files and check what django is doing with these fields.
If django is doing migrations.AlterField for the field which was a choices field and is now a manytomanyfield, change it to
migrations.RemoveField and then Migrations.AddField
- How to use the user_passes_test decorator in class based views?
- Django-Rest-Framework serializer class meta
- Passing list of values to django view via jQuery ajax call
0👍
I had no meaningful data as yet, so I deleted the database(db.sqlite3) and all migrations(0001,0002 etc)
- What is choice_set.all in Django tutorial
- Django rest framework: 'create( )' NotImplementedError when making Http POST request
- How to avoid putting environment variables into multiple places with Django, nginx and uWSGI?
- What is the opposite of @login_required decorator for Django views?
0👍
Simply check if you renamed model-related attributes or anthing related to the model in the admin before you drop the db.
Actually it worked for me.