[Django] #28141: When have null=True inside ForeignKey, Django don't to do CASCADE Operation even passing on_delete=models.CASCADE. [using PostgreSQL]

8 views
Skip to first unread message

Django

unread,
Apr 27, 2017, 11:35:17 AM4/27/17
to django-...@googlegroups.com
#28141: When have null=True inside ForeignKey, Django don't to do CASCADE Operation
even passing on_delete=models.CASCADE. [using PostgreSQL]
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
mateuspadua |
Type: | Status: new
Uncategorized |
Component: Database | Version: 1.10
layer (models, ORM) | Keywords: ForeignKey null
Severity: Normal | True PostgreSQL
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Example:

from cms.models import Page


class OfferingPlugin(CMSPlugin):
image = VersatileImageField(upload_to=unique_upload,
ppoi_field='ppoi')
ppoi = PPOIField()
title = models.CharField(max_length=50)
subtitle = models.CharField(max_length=140)
link_copy = models.CharField(max_length=20)
link_address = models.ForeignKey(Page, blank=True, null=True,
on_delete=models.CASCADE) ** -> dont work because has null=True**

....

page = Page.objecs.first()

If I to try delete a Page instance, for example page.delete() the error
bellow will occur:

.....

IntegrityError: insert or update on table "offering_offeringplugin"
violates foreign key constraint
"offering_offering_cmsplugin_ptr_id_9a9e9998_fk_cms_cmsplugin_id"
DETAIL: Key (id)=(10) is still referenced from table
"offering_offeringplugin".

--
Ticket URL: <https://code.djangoproject.com/ticket/28141>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 27, 2017, 11:36:08 AM4/27/17
to django-...@googlegroups.com
#28141: When have null=True inside ForeignKey, Django don't to do CASCADE Operation
even passing on_delete=models.CASCADE. [using PostgreSQL]
-------------------------------------+-------------------------------------
Reporter: mateuspadua | Owner: nobody
Type: Uncategorized | Status: new
Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution:
Keywords: ForeignKey null | Triage Stage:
True PostgreSQL | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by mateuspadua:

Old description:

> Example:
>
> from cms.models import Page
>

> class OfferingPlugin(CMSPlugin):
> image = VersatileImageField(upload_to=unique_upload,
> ppoi_field='ppoi')
> ppoi = PPOIField()
> title = models.CharField(max_length=50)
> subtitle = models.CharField(max_length=140)
> link_copy = models.CharField(max_length=20)
> link_address = models.ForeignKey(Page, blank=True, null=True,
> on_delete=models.CASCADE) ** -> dont work because has null=True**
>
> ....
>
> page = Page.objecs.first()
>
> If I to try delete a Page instance, for example page.delete() the error
> bellow will occur:
>
> .....
>
> IntegrityError: insert or update on table "offering_offeringplugin"
> violates foreign key constraint
> "offering_offering_cmsplugin_ptr_id_9a9e9998_fk_cms_cmsplugin_id"
> DETAIL: Key (id)=(10) is still referenced from table
> "offering_offeringplugin".

New description:

Example:


{{{
from cms.models import Page


class OfferingPlugin(CMSPlugin):
image = VersatileImageField(upload_to=unique_upload,
ppoi_field='ppoi')
ppoi = PPOIField()
title = models.CharField(max_length=50)
subtitle = models.CharField(max_length=140)
link_copy = models.CharField(max_length=20)
link_address = models.ForeignKey(Page, blank=True, null=True,
on_delete=models.CASCADE) ** -> dont work because has null=True**

....

page = Page.objecs.first()

}}}

If I to try delete a Page instance, for example page.delete() the error
bellow will occur:

.....

IntegrityError: insert or update on table "offering_offeringplugin"
violates foreign key constraint
"offering_offering_cmsplugin_ptr_id_9a9e9998_fk_cms_cmsplugin_id"
DETAIL: Key (id)=(10) is still referenced from table
"offering_offeringplugin".

--

--
Ticket URL: <https://code.djangoproject.com/ticket/28141#comment:1>

Django

unread,
Apr 27, 2017, 11:37:09 AM4/27/17
to django-...@googlegroups.com
#28141: When have null=True inside ForeignKey, Django don't to do CASCADE Operation
even passing on_delete=models.CASCADE. [using PostgreSQL]
-------------------------------------+-------------------------------------
Reporter: mateuspadua | Owner: nobody
Type: Uncategorized | Status: new
Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution:
Keywords: ForeignKey null | Triage Stage:
True PostgreSQL | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by mateuspadua:

Old description:

> Example:
>

> {{{
> from cms.models import Page
>

> class OfferingPlugin(CMSPlugin):
> image = VersatileImageField(upload_to=unique_upload,
> ppoi_field='ppoi')
> ppoi = PPOIField()
> title = models.CharField(max_length=50)
> subtitle = models.CharField(max_length=140)
> link_copy = models.CharField(max_length=20)
> link_address = models.ForeignKey(Page, blank=True, null=True,
> on_delete=models.CASCADE) ** -> dont work because has null=True**
>
> ....
>
> page = Page.objecs.first()
>
> }}}
>
> If I to try delete a Page instance, for example page.delete() the error
> bellow will occur:
>
> .....
>
> IntegrityError: insert or update on table "offering_offeringplugin"
> violates foreign key constraint
> "offering_offering_cmsplugin_ptr_id_9a9e9998_fk_cms_cmsplugin_id"
> DETAIL: Key (id)=(10) is still referenced from table
> "offering_offeringplugin".

New description:

Example:


{{{
from cms.models import Page


class OfferingPlugin(models.Model):


image = VersatileImageField(upload_to=unique_upload,
ppoi_field='ppoi')
ppoi = PPOIField()
title = models.CharField(max_length=50)
subtitle = models.CharField(max_length=140)
link_copy = models.CharField(max_length=20)
link_address = models.ForeignKey(Page, blank=True, null=True,
on_delete=models.CASCADE) ** -> dont work because has null=True**

....

page = Page.objecs.first()

}}}

If I to try delete a Page instance, for example page.delete() the error
bellow will occur:

.....

IntegrityError: insert or update on table "offering_offeringplugin"
violates foreign key constraint
"offering_offering_cmsplugin_ptr_id_9a9e9998_fk_cms_cmsplugin_id"
DETAIL: Key (id)=(10) is still referenced from table
"offering_offeringplugin".

--

--
Ticket URL: <https://code.djangoproject.com/ticket/28141#comment:2>

Django

unread,
Apr 27, 2017, 11:37:30 AM4/27/17
to django-...@googlegroups.com
#28141: When have null=True inside ForeignKey, Django don't to do CASCADE Operation
even passing on_delete=models.CASCADE. [using PostgreSQL]
-------------------------------------+-------------------------------------
Reporter: mateuspadua | Owner: nobody
Type: Uncategorized | Status: new
Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution:
Keywords: ForeignKey null | Triage Stage:
True PostgreSQL | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by mateuspadua:

Old description:

> Example:
>

> {{{
> from cms.models import Page
>

> class OfferingPlugin(models.Model):


> image = VersatileImageField(upload_to=unique_upload,
> ppoi_field='ppoi')
> ppoi = PPOIField()
> title = models.CharField(max_length=50)
> subtitle = models.CharField(max_length=140)
> link_copy = models.CharField(max_length=20)
> link_address = models.ForeignKey(Page, blank=True, null=True,
> on_delete=models.CASCADE) ** -> dont work because has null=True**
>
> ....
>
> page = Page.objecs.first()
>
> }}}
>
> If I to try delete a Page instance, for example page.delete() the error
> bellow will occur:
>
> .....
>
> IntegrityError: insert or update on table "offering_offeringplugin"
> violates foreign key constraint
> "offering_offering_cmsplugin_ptr_id_9a9e9998_fk_cms_cmsplugin_id"
> DETAIL: Key (id)=(10) is still referenced from table
> "offering_offeringplugin".

New description:

Example:


{{{
from cms.models import Page


class OfferingPlugin(models.Model):


title = models.CharField(max_length=50)
subtitle = models.CharField(max_length=140)
link_copy = models.CharField(max_length=20)
link_address = models.ForeignKey(Page, blank=True, null=True,
on_delete=models.CASCADE) ** -> dont work because has null=True**

....

page = Page.objecs.first()

}}}

If I to try delete a Page instance, for example page.delete() the error
bellow will occur:

.....

IntegrityError: insert or update on table "offering_offeringplugin"
violates foreign key constraint
"offering_offering_cmsplugin_ptr_id_9a9e9998_fk_cms_cmsplugin_id"
DETAIL: Key (id)=(10) is still referenced from table
"offering_offeringplugin".

--

--
Ticket URL: <https://code.djangoproject.com/ticket/28141#comment:3>

Django

unread,
Apr 27, 2017, 11:51:41 AM4/27/17
to django-...@googlegroups.com
#28141: When have null=True inside ForeignKey, Django don't to do CASCADE Operation
even passing on_delete=models.CASCADE. [using PostgreSQL]
-------------------------------------+-------------------------------------
Reporter: mateuspadua | Owner: nobody
Type: Uncategorized | Status: new
Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution:
Keywords: ForeignKey null | Triage Stage:
True PostgreSQL | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham):

Could you please provide code to reproduce the issue (a sample project,
for example) that doesn't involve a third-party project (django-cms in
this case) so we can rule out a bug in that package?

--
Ticket URL: <https://code.djangoproject.com/ticket/28141#comment:4>

Django

unread,
Apr 27, 2017, 2:29:03 PM4/27/17
to django-...@googlegroups.com
#28141: When have null=True inside ForeignKey, Django don't to do CASCADE Operation
even passing on_delete=models.CASCADE. [using PostgreSQL]
-------------------------------------+-------------------------------------
Reporter: mateuspadua | Owner: nobody
Type: Uncategorized | Status: new
Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution:
Keywords: ForeignKey null | Triage Stage:
True PostgreSQL | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by mateuspadua):

Thanks for replay:

I did more tests and i figure out that my constraint was without the
options Deferrable and Deferred assigned, because my current DB PostgreSQL
was migrate from MySQL. Then I assigned this options manually on DB and
worked.

[[Image(https://drive.google.com/file/d/0B7FKYDMJBG3bVVBCM1k3X0tsUEk/view?usp=sharing)]]

More information about this options on the link bellow:
http://stackoverflow.com/questions/16323236/deferrable-check-constraint-
in-postgresql

I hope this can help.

--
Ticket URL: <https://code.djangoproject.com/ticket/28141#comment:5>

Django

unread,
Apr 27, 2017, 2:30:55 PM4/27/17
to django-...@googlegroups.com
#28141: When have null=True inside ForeignKey, Django don't to do CASCADE Operation
even passing on_delete=models.CASCADE. [using PostgreSQL]
-------------------------------------+-------------------------------------
Reporter: mateuspadua | Owner: nobody
Type: Uncategorized | Status: closed

Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: ForeignKey null | Triage Stage:
True PostgreSQL | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by mateuspadua):

* status: new => closed
* resolution: => fixed


--
Ticket URL: <https://code.djangoproject.com/ticket/28141#comment:6>

Django

unread,
Apr 27, 2017, 3:29:37 PM4/27/17
to django-...@googlegroups.com
#28141: When have null=True inside ForeignKey, Django don't to do CASCADE Operation
even passing on_delete=models.CASCADE. [using PostgreSQL]
-------------------------------------+-------------------------------------
Reporter: mateuspadua | Owner: nobody
Type: Uncategorized | Status: closed
Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution: invalid

Keywords: ForeignKey null | Triage Stage:
True PostgreSQL | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

* resolution: fixed => invalid


--
Ticket URL: <https://code.djangoproject.com/ticket/28141#comment:7>

Django

unread,
Apr 27, 2017, 9:45:36 PM4/27/17
to django-...@googlegroups.com
#28141: When have null=True inside ForeignKey, Django don't to do CASCADE Operation
even passing on_delete=models.CASCADE. [using PostgreSQL]
-------------------------------------+-------------------------------------
Reporter: mateuspadua | Owner: nobody
Type: Uncategorized | Status: closed
Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: ForeignKey null | Triage Stage:
True PostgreSQL | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* status: new => closed


--
Ticket URL: <https://code.djangoproject.com/ticket/28141#comment:8>

Django

unread,
Apr 27, 2017, 9:45:58 PM4/27/17
to django-...@googlegroups.com
#28141: When have null=True inside ForeignKey, Django don't to do CASCADE Operation
even passing on_delete=models.CASCADE. [using PostgreSQL]
-------------------------------------+-------------------------------------
Reporter: mateuspadua | Owner: nobody
Type: Uncategorized | Status: closed
Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: ForeignKey null | Triage Stage:
True PostgreSQL | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* status: new => closed

* resolution: => invalid


--
Ticket URL: <https://code.djangoproject.com/ticket/28141#comment:6>

Reply all
Reply to author
Forward
0 new messages