Closed Bug 696429 Opened 13 years ago Closed 13 years ago

[traceback] IntegrityError: (1451, 'Cannot delete or update a parent row: a foreign key constraint fails (`addons_test`.`addons_users`, CONSTRAINT `addons_users_ibfk_1` FOREIGN KEY (`addon_id`) REFERENCES `addons` (`id`))')

Categories

(addons.mozilla.org Graveyard :: Developer Pages, defect, P2)

defect

Tracking

(Not tracked)

RESOLVED WONTFIX

People

(Reporter: krupa.mozbugs, Assigned: robhudson)

References

()

Details

steps to reproduce:
Try to delete a webapp

traceback:
Traceback (most recent call last):

 File "/data/www/addons-dev.allizom.org/zamboni/vendor/src/django/django/core/handlers/base.py", line 111, in get_response
   response = callback(request, *callback_args, **callback_kwargs)

 File "/data/www/addons-dev.allizom.org/zamboni/apps/amo/decorators.py", line 102, in wrapper
   return f(*args, **kw)

 File "/data/www/addons-dev.allizom.org/zamboni/apps/amo/decorators.py", line 94, in wrapper
   return f(*args, **kw)

 File "/data/www/addons-dev.allizom.org/zamboni/apps/addons/decorators.py", line 25, in wrapper
   return f(request, addon, *args, **kw)

 File "/data/www/addons-dev.allizom.org/zamboni/apps/amo/decorators.py", line 28, in wrapper
   return func(request, *args, **kw)

 File "/data/www/addons-dev.allizom.org/zamboni/apps/devhub/decorators.py", line 32, in wrapper
   return fun()

 File "/data/www/addons-dev.allizom.org/zamboni/apps/devhub/decorators.py", line 24, in <lambda>
   **kw)

 File "/data/www/addons-dev.allizom.org/zamboni/apps/devhub/views.py", line 278, in delete
   addon.delete('Removed via devhub')

 File "/data/www/addons-dev.allizom.org/zamboni/vendor/src/django/django/db/transaction.py", line 211, in inner
   return func(*args, **kwargs)

 File "/data/www/addons-dev.allizom.org/zamboni/apps/addons/models.py", line 351, in delete
   rv = super(Addon, self).delete()

 File "/data/www/addons-dev.allizom.org/zamboni/vendor/src/django/django/db/models/base.py", line 584, in delete
   collector.delete()

 File "/data/www/addons-dev.allizom.org/zamboni/vendor/src/django/django/db/models/deletion.py", line 62, in decorated
   func(self, *args, **kwargs)

 File "/data/www/addons-dev.allizom.org/zamboni/vendor/src/django/django/db/models/deletion.py", line 253, in delete
   query.delete_batch(pk_list, self.using)

 File "/data/www/addons-dev.allizom.org/zamboni/vendor/src/django/django/db/models/sql/subqueries.py", line 44, in delete_batch
   self.do_query(self.model._meta.db_table, where, using=using)

 File "/data/www/addons-dev.allizom.org/zamboni/vendor/src/django/django/db/models/sql/subqueries.py", line 29, in do_query
   self.get_compiler(using).execute_sql(None)

 File "/data/www/addons-dev.allizom.org/zamboni/vendor/src/django/django/db/models/sql/compiler.py", line 750, in execute_sql
   cursor.execute(sql, params)

 File "/data/www/addons-dev.allizom.org/zamboni/vendor/src/django/django/db/backends/mysql/base.py", line 86, in execute
   return self.cursor.execute(query, args)

 File "/usr/lib64/python2.6/site-packages/MySQLdb/cursors.py", line 173, in execute
   self.errorhandler(self, exc, value)

 File "/usr/lib64/python2.6/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
   raise errorclass, errorvalue

IntegrityError: (1451, 'Cannot delete or update a parent row: a foreign key constraint fails (`addons_test`.`addons_users`, CONSTRAINT `addons_users_ibfk_1` FOREIGN KEY (`addon_id`) REFERENCES `addons` (`id`))')
Priority: -- → P2
Assignee: nobody → ashort
Target Milestone: --- → 6.2.9
See also bug 679869.
Assignee: ashort → chudson
Target Milestone: 6.2.9 → 6.3.0
Do we want to "hard" delete addons? Or "soft" delete them... e.g. amo.STATUS_DELETED?
I'm pretty sure we're hitting this Django bug:
https://code.djangoproject.com/ticket/17140

Basically, the `ForeignKey`s are referring to Addon and not Webapp, so Django doesn't know it has to delete them before deleting the Webapp.  To Django, the model doesn't match so there is no relationship so it skips it, which ultimately results in the integrity error that we see.

Options as I see it are:

1) Add the `ON CASCADE DELETE`s to MySQL to handle this for apps.
2) Help Django developers fix this bug, or find a workaround in our code base.
3) Other?
After more investigation, we may or may no be hitting this bug specifically (I wrote a Django test and couldn't get it to fail), but it is rearing its head because we are changing the class type in Addon.__new__ to be class type Webapp.

I tested disabling the __new__ hack and deletes worked just fine.
Fixing the bug so that on_delete works with proxy models would be great.
I think our changing of class types in __new__ is uncommon and is ultimately the reason this fails.  I wouldn't expect Django to support this use case.  If I remove the __new__ code it works as expected.  I'm not sure the 17140 bug is valid or if it is, it doesn't apply to how we're using proxy models?
Wontfix'ing since we're implementing soft deletes. This should get fixed by upstream Django eventually. If for some reason we need it later, we can check on the status of https://code.djangoproject.com/ticket/17140 and related bugs.
Status: NEW → RESOLVED
Closed: 13 years ago
Resolution: --- → WONTFIX
Product: addons.mozilla.org → addons.mozilla.org Graveyard
You need to log in before you can comment on or make changes to this bug.