IntegrityError: foreign key violation on deletion

I have an order and dispatch model. Shipment has a foreign key for ordering.

class Order(...):
   ...

class Shipment()
   order = m.ForeignKey('Order')
   ...

Now, in one of my views, I want to delete the order object along with all related objects. Therefore, I call order.delete ().

I have Django 1.0.4, PostgreSQL 8.4, and I use transaction middleware, so the whole query is enclosed in a single transaction.

The problem is that after order.delete () I get:

...
File "/usr/local/lib/python2.6/dist-packages/django/db/backends/__init__.py", line 28, in _commit
return self.connection.commit()

IntegrityError: update or delete on table "main_order" violates 
foreign key constraint "main_shipment_order_id_fkey" on table "main_shipment"
DETAIL:  Key (id)=(45) is still referenced from table "main_shipment".

I checked in connection.queries that the correct requests are being executed in the correct order. The first dispatch is deleted, after which django deletes the order line:

{'time': '0.000', 'sql': 'DELETE FROM "main_shipment" WHERE "id" IN (17)'},
{'time': '0.000', 'sql': 'DELETE FROM "main_order" WHERE "id" IN (45)'}

The foreign key has ON DELETE NO ACTION (default) and is initially deferred. I do not know why I am violating foreign key restrictions.

pre_delete delete , .

DELETE Postgres, , , - , .

, Cart, id, cart_ptr_id DELETE DELETE , ? - > , .

+5
1

: (id) = (45) - "main_shipment".

, id 45. 17 main_shipment , . main_shipment, id 45 main_order. , .

+4

All Articles