Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django ignores on_delete=Cascade when creating Postgresql-DBs

I use django 1.7 to create a postgresql 9.3 db with several tables that contain foreign-key constraints. The DB is used for a warehouse in which objects have a physical location. If an object (in table Stored_objects) is deleted, I'd also would like to delete it's position, so that my model for the location looks like:

class object_positions(models.Model):

  obj_id = models.ForeignKey(Stored_objects, db_column='obj_id',on_delete=models.CASCADE)
  (...)

The constraint in the db (after syncdb) however looks like this:

ALTER TABLE object_positions
  ADD CONSTRAINT stored_obj_fkey FOREIGN KEY (obj_id)
      REFERENCES "Stored_objects" (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION;

Is there something else I have to do to get this constraint right in the db?

like image 202
FooTheBar Avatar asked Jun 25 '15 10:06

FooTheBar


Video Answer


1 Answers

Django uses it's own code to handle cascades, they're not implemented on a database level. The main reason is to maintain consistent behaviour across backends, and to allow model signals to fire for cascaded deletions. If for some reason you want the constraint on a database level, you'll have to edit the table yourself. I wouldn't recommend that unless you have a compelling reason (such as another app accessing the database, bypassing Django).

like image 68
knbk Avatar answered Oct 30 '22 20:10

knbk