Deleting user cause integrity error ... violates foreign key

1,172 views
Skip to first unread message

dbee

unread,
Sep 19, 2008, 7:51:50 AM9/19/08
to Django users
I'm trying to delete a user from my admin panel. The system tells me
that it causes an integrity error by violating a foreign key
constraint ...

What causes these kinds of errors ?

IntegrityError at /admin/auth/user/28/delete/
ERROR: update or delete on "mot1_campaigns" violates foreign key
constraint "$1" on "mobt1_response" DETAIL: Key (id)=(18) is still
referenced from table "mot1_response". DELETE FROM "mot1_campaigns"
WHERE "id" IN (18)

Environment:

Request Method: POST
Request URL: http://mot.com/admin/auth/user/28/delete/
Django Version: 0.97-pre-SVN-unknown
Python Version: 2.3.4
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'mot.mot1',
'mot.fckeditor']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.doc.XViewMiddleware',
'django.middleware.transaction.TransactionMiddleware')


Traceback:
File "/usr/lib/python2.3/site-packages/django/core/handlers/base.py"
in get_response
82. response = callback(request, *callback_args,
**callback_kwargs)
File "/usr/lib/python2.3/site-packages/django/contrib/admin/views/
decorators.py" in _checklogin
62. return view_func(request, *args, **kwargs)
File "/usr/lib/python2.3/site-packages/django/views/decorators/
cache.py" in _wrapped_view_func
44. response = view_func(request, *args, **kwargs)
File "/usr/lib/python2.3/site-packages/django/contrib/admin/views/
main.py" in delete_stage
522. obj.delete()
File "/usr/lib/python2.3/site-packages/django/db/models/base.py" in
delete
399. delete_objects(seen_objs)
File "/usr/lib/python2.3/site-packages/django/db/models/query.py" in
delete_objects
705. del_query.delete_batch(pk_list)
File "/usr/lib/python2.3/site-packages/django/db/models/sql/
subqueries.py" in delete_batch
86. self.do_query(self.model._meta.db_table, where)
File "/usr/lib/python2.3/site-packages/django/db/models/sql/
subqueries.py" in do_query
35. self.execute_sql(None)
File "/usr/lib/python2.3/site-packages/django/db/models/sql/query.py"
in execute_sql
1466. cursor.execute(sql, params)
File "/usr/lib/python2.3/site-packages/django/db/backends/util.py" in
execute
18. return self.cursor.execute(sql, params)
File "/usr/lib/python2.3/site-packages/django/db/backends/postgresql/
base.py" in execute
47. return self.cursor.execute(smart_str(sql, self.charset),
self.format_params(params))

Exception Type: IntegrityError at /admin/auth/user/28/delete/
Exception Value: ERROR: update or delete on "mot1_campaigns" violates
foreign key constraint "$1" on "mot1_response"
DETAIL: Key (id)=(18) is still referenced from table "mot1_response".

DELETE FROM "mot1_campaigns" WHERE "id" IN (18)

Hanne Moa

unread,
Sep 22, 2008, 4:32:15 AM9/22/08
to django...@googlegroups.com
On Fri, Sep 19, 2008 at 1:51 PM, dbee <darab...@gmail.com> wrote:
>
> I'm trying to delete a user from my admin panel. The system tells me
> that it causes an integrity error by violating a foreign key
> constraint ...
>
> What causes these kinds of errors ?
>
> IntegrityError at /admin/auth/user/28/delete/
> ERROR: update or delete on "mot1_campaigns" violates foreign key
> constraint "$1" on "mobt1_response" DETAIL: Key (id)=(18) is still
> referenced from table "mot1_response". DELETE FROM "mot1_campaigns"
> WHERE "id" IN (18)

The row/object with id 18 in the "mot1_campaigns" is still stored in
the table "mot1_response" where the the field is not set as null. What
you could do is add an "ON DELETE CASCADE" on the foreign key in
"mot1_response", if you sql-server supports that. Then when you
delete, the row/object in "mot1_response" will be automatically
deleted also. You can't set this from inside Django though so it's
time to read the fine sql manual. If your server does not support ON
DELETE CASCADE you need to delete all dependent rows/objects manually
before you delete the user.


HM

Reply all
Reply to author
Forward
0 new messages