If you search about it on the Internet many sources claim that Django
does not set those constraints on the database level. 
If you look at the code of the base code for database backends, you'll
notice that they don't mention on_delete at all. - https://github.com/d
jango/django/blob/6dd4edb1b4f5441c5f543e29395039839c50d10b/django/db/ba
ckends/base/schema.py#L983-L1000 - https://github.com/django/django/blo
b/6dd4edb1b4f5441c5f543e29395039839c50d10b/django/db/backends/base/sche
ma.py#L72-L75
And postgres does not seem to override that behaviour - https://github.
com/django/django/blob/6dd4edb1b4f5441c5f543e29395039839c50d10b/django/
db/backends/postgresql/schema.py
I may be looking in a wrong place, but I think it's just Django not
using database-leve constraints. As long as you use Django's ORM they
are enforced, I guess.

On Fri, 2018-06-22 at 15:56 -0700, James Bellaby wrote:
> I'm having a little trouble with my "through" table. I've set the
> modal to have two Foreign Key fields. Ex. below:
> 
> class Person(models.Model):
>     title = models.CharField(max_length=255)
> 
> 
> class Group(models.Model):
>     title = models.CharField(max_length=255)
>     members= models.ManyToManyField(Person, through='Membership',
> through_fields=('group', 'person'))
> 
> 
> class Membership(models.Model):
>     group = models.ForeignKey(Group, on_delete=models.CASCADE)
>     person = models.ForeignKey(Person, on_delete=models.PROTECT)
> 
> 
> 
> 
> I want it to keep the "Person" entries when deleted but if a group is
> deleted in needs to cascade.
> 
> However when looking are the SQL in Postgresql it's created the
> Membership table constraint for the Group id with "ON DELETE NO
> ACTION"
> 
> CONSTRAINT groups_membership_group_id_d4404a8c_fk_groups_group_id
> FOREIGN KEY (group_id)
>         REFERENCES public.groups_group (id) MATCH SIMPLE
>         ON UPDATE NO ACTION
>         ON DELETE NO ACTION
>         DEFERRABLE INITIALLY DEFERRED,
> 
> 
> My understanding was that setting CASCADE on the group in the
> Membership modal would set ON DELETE CASCADE for the Membership table
> CREATE?
> 
> If it's not a bug and I've missed something, I apologies in advance.
> 
> Cheers,
> James
> 
> 
> 
> 
> 
> 
> -- 
> 
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> 
> To unsubscribe from this group and stop receiving emails from it,
> send an email to django-users+unsubscr...@googlegroups.com.
> 
> To post to this group, send email to django-users@googlegroups.com.
> 
> Visit this group at https://groups.google.com/group/django-users.
> 
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/d489db86-aeea-4272-9fc9-
> a1d5ffcd3736%40googlegroups.com.
> 
> For more options, visit https://groups.google.com/d/optout.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/aa10ba0d32f25c5cea8538d3812e52d2875ac971.camel%40tmkn.org.
For more options, visit https://groups.google.com/d/optout.

Reply via email to