Django ignores on_delete = Cascade when creating Postgresql-DB

I am using django 1.7 to create postgresql 9.3 db with multiple tables that contain foreign key constraints. The database is used for a warehouse in which objects have a physical location. If the object (in the Stored_objects table) is deleted, I would also like to delete it so that my model for the location looks like this:

class object_positions(models.Model):

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

      

The db limit (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 need to do to get this constraint right into the db?

+3


source to share


1 answer


Django uses native code to handle cascades, they are not implemented at the database level. The main reason is to maintain consistent behavior across the server and to allow model signals for cascading exceptions. If for some reason you require a database-level constraint, you will have to edit the table yourself. I would not recommend this unless you have a good reason (like another application accessing the database, bypassing Django).



+4


source







All Articles