Saltar al contenido

Ejemplo de código en cascada de eliminación de DJANGO ON

Hacemos una revisión completa cada secciones de nuestra web con el objetivo de enseñarte en todo momento la información con la mayor veracidad y certera.

Ejemplo 1: on_delete opciones django

There are seven possible actions to take when such event occurs:

CASCADE: When the referenced objectis deleted, also delete the objects that have references to it 
  (when you remove a blog post for instance, you might want to delete comments as well). 
  SQL equivalent: CASCADE.
      
PROTECT: Forbid the deletion of the referenced object. 
  To delete it you will have to delete all objects that reference it manually. 
  SQL equivalent: RESTRICT.
    
RESTRICT:(introduced in Django 3.1) Similar behavior as PROTECT that matches SQL's RESTRICT more accurately.(See django documentation example)
SET_NULL: Set the reference to NULL (requires the field to be nullable). 
  For instance, when you delete a User, you might want to keep the comments he posted on blog posts, 
  but say it was posted by an anonymous (or deleted) user. 
  SQL equivalent: SET NULL.
    
SET_DEFAULT: Set the default value. SQL equivalent: SET DEFAULT.
    
SET(...): Set a given value. This one isnot part of the SQL standard andis entirely handled by Django.
  
DO_NOTHING: Probably a very bad idea since this would create integrity issues in your database 
  (referencing an object that actually doesn't exist). SQL equivalent: NO ACTION.

Ejemplo 2: on_delete=models.cascade

on_delete=models.CASCADE will delete anything created by the admin if
the admin user is deleted.

Ejemplo 3: en cascada de eliminación

CREATE TABLE child_table
(
  column1 datatype [ NULL | NOT NULL ],
  column2 datatype [ NULL | NOT NULL ],...

  CONSTRAINT fk_name
    FOREIGN KEY (child_col1, child_col2,... child_col_n)
    REFERENCES parent_table (parent_col1, parent_col2,... parent_col_n)
    ON DELETE CASCADE
    [ ON UPDATE  NO ACTION ]);

valoraciones y reseñas

Agradecemos que quieras añadir valor a nuestra información colaborando tu veteranía en las ilustraciones.

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *