Hi everyone,

I've encountered an issue when working with django and I can't seem to find 
a way out of this..

I am using django 1.7.2 and a MySQL database. 
I have a model that was using a One-to-One relationship with two other 
models. At first I thought, the One-to-One relationship will be enough, but 
as things changed in the project I had to change the relationship to 
Many-to-One.

I changed only two lines in my code, in my model I had:

product = models.OneToOneField(Product)
category = models.OneToOneField(Category)

and changed it to:

product = models.ForeignKey(Product)
category = models.ForeignKey(Category)

I created the db migrations using ./manage.py makemigrations, but when I 
run the migration with 'migrate' it keeps throwing this error. 

django.db.utils.OperationalError: (1553, "Cannot drop index 'product_id': 
needed in a foreign key constraint")

I tried to check what sql operations are executed in this case and it seems 
that django tries to run 
ALTER TABLE 'xxx' DROP INDEX 'yyy' and
ALTER TABLE `xxx` DROP FOREIGN KEY `xxx_yyy_id_{some_hash}_fk_yyy_id`;

I am not sure if this is the right order for these operations, when running 
it manually on the database in the reverse order (first the DROP FOREIGN 
KEY then DROP INDEX) it works correctly. 

Have you encountered this issue before? Have you got any suggestions on how 
to resolve this situation? 
I would like to avoid any manual SQL changes outside of django migrations. 

Thanks in advance. 

Best regards, 
Maciej Szopiński

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/d7318f2a-6583-40e7-be1d-f8654c75c8e4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to