Monday, June 25, 2018

Re: Django Postgres Intermediary doesn't set ON DELETE CASCADE

Hey James,

Just to add to what was already said there's a ticket tracking the addition of
database level foreign constraints.[0]

Cheers,
Simon

[0] https://code.djangoproject.com/ticket/21961

Le samedi 23 juin 2018 09:56:23 UTC-4, Tomasz Knapik a écrit :
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.


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...@googlegroups.com.
To post to this group, send email to django...@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+unsubscribe@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/e55e3d08-426a-498a-9f98-b93ae4c0b229%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment