Reputation: 3433
I have the following model:
class GeneMiRNACombination(models.Model):
gene = models.CharField(max_length=50)
experiment = models.ForeignKey(Experiment, on_delete=models.CASCADE)
source_statistical_data = models.OneToOneField(
SourceDataStatisticalProperties,
on_delete=models.SET_NULL,
blank=True,
null=True,
default=None
)
So I'd expect tha the ON_DELETE
will be setted in the DB (Postgres in my case). But when I see the CREATE script this is what it prints:
CREATE TABLE public.gene_mirna_combination
(
id integer NOT NULL DEFAULT nextval('gene_mirna_combination_id_seq'::regclass),
gene character varying(50) COLLATE pg_catalog."default" NOT NULL,
experiment_id integer NOT NULL,
source_statistical_data_id integer,
CONSTRAINT gene_mirna_combination_pkey PRIMARY KEY (id),
CONSTRAINT gene_mirna_combination_properties_id_key UNIQUE (source_statistical_data_id),
CONSTRAINT gene_mirna_combinati_experiment_id_31b52a17_fk_api_servi FOREIGN KEY (experiment_id)
REFERENCES public.api_service_experiment (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
DEFERRABLE INITIALLY DEFERRED,
CONSTRAINT gene_mirna_combinati_source_statistical_d_7d0f04a8_fk_statistic FOREIGN KEY (source_statistical_data_id)
REFERENCES public.statistical_properties_sourcedatastatisticalproperties (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
DEFERRABLE INITIALLY DEFERRED
)
TABLESPACE pg_default;
ALTER TABLE public.gene_mirna_combination
OWNER to root;
CREATE INDEX gene_mirna_combination_experiment_id_31b52a17
ON public.gene_mirna_combination USING btree
(experiment_id ASC NULLS LAST)
TABLESPACE pg_default;
So, there is no action on delete! Why? It should set the ON DELETE CASCADE
when creates the table, shouldn't it? Is there any implicit behaviour?
If there isn't an ON DELETE policy so every time I remove a referenced object it must be removed from Django, which will be extremely slower than a SQL robust solution
Upvotes: 0
Views: 119
Reputation: 1860
Django does not apply constraints to the the DB schema; same for server defaults for instance.
To be really honest I hate this behaviour but .... Django works in that way and we have to leave with it
UPDATE
You can follow the discussion about this PR and the related feature request
Upvotes: 1