Opened 5 years ago

Closed 5 years ago

#28141 closed Uncategorized (invalid)

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

Reported by: mateuspadua Owned by: nobody
Component: Database layer (models, ORM) Version: 1.10
Severity: Normal Keywords: ForeignKey null True PostgreSQL
Cc: mateuspaduaweb@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by mateuspadua)

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".

Change History (6)

comment:1 Changed 5 years ago by mateuspadua

Description: modified (diff)

comment:2 Changed 5 years ago by mateuspadua

Description: modified (diff)

comment:3 Changed 5 years ago by mateuspadua

Description: modified (diff)

comment:4 Changed 5 years ago 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?

comment:5 Changed 5 years ago 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.

https://drive.google.com/file/d/0B7FKYDMJBG3bVVBCM1k3X0tsUEk/view

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

I hope this can help.

comment:6 Changed 5 years ago by Tim Graham

Resolution: invalid
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top