[Django] #25148: Add a warning for `CASCADE`ing foreign keys on custom User models.

25 views
Skip to first unread message

Django

unread,
Jul 20, 2015, 1:03:18 PM7/20/15
to django-...@googlegroups.com
#25148: Add a warning for `CASCADE`ing foreign keys on custom User models.
-------------------------------+--------------------
Reporter: fcurella | Owner: nobody
Type: Uncategorized | Status: new
Component: contrib.auth | Version: 1.8
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------
Assume a custom user models is created as such:

{{{
class Taco(models.Model):
flavor = models.CharField(max_length=64, unique=True)


class User(AbstractBaseUser):
USERNAME_FIELD = 'username'

username = models.CharField(max_length=64, unique=True)
favorite_taco = models.ForeignKey(Taco, null=True)
}}}

When deleting the related objects, the user will also be deleted because
is cascading:

{{{
taco = Taco.objects.create(flavor="vegetarian")
user = User.objects.create_user(
username='taco fan',
favorite_taco=taco
)
User.objects.count() # 1
taco.delete()
User.objects.count() # 0
}}}

This is intended behavior, but it's still pretty dangerous and a common
trap for beginners.

I'd like to leverage the system check framework to add a Warning whenever
a FK with `on_delete=CASCADE` is added to a User model. Something along
the lines of:

{{{
class AbstractUser():
@classmethod
def check(cls, **kwargs):
errors = super(AbstractUser, cls).check(**kwargs)
for field in cls._meta.fields:
if isinstance(field, models.ForeignKey):
if field.rel.on_delete == models.CASCADE:
errors.append(checks.Warning(
"Field {0} is set to CASCADE on delete. You might
lose users".format(field.name),
hint="Explicitly set {0}.on_delete to something
else than `models.CASCADE".format(field.name),
obj=field,
id="{0}.W001".format(cls._meta.app_label)
))
return errors
}}}

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

Django

unread,
Jul 20, 2015, 1:04:30 PM7/20/15
to django-...@googlegroups.com
#25148: Add a warning for `CASCADE`ing foreign keys on custom User models.
-------------------------------+--------------------------------------

Reporter: fcurella | Owner: nobody
Type: Uncategorized | Status: new
Component: contrib.auth | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by fcurella):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Old description:

New description:

Assume a custom user models is created as such:

{{{
class Taco(models.Model):
flavor = models.CharField(max_length=64, unique=True)


class User(AbstractBaseUser):
USERNAME_FIELD = 'username'

username = models.CharField(max_length=64, unique=True)
favorite_taco = models.ForeignKey(Taco, null=True)
}}}

When deleting the related object, the user will also be deleted because of
cascading:

--

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

Django

unread,
Jul 20, 2015, 1:04:39 PM7/20/15
to django-...@googlegroups.com
#25148: Add a warning for `CASCADE`ing foreign keys on custom User models.
-------------------------------+--------------------------------------
Reporter: fcurella | Owner: fcurella
Type: Uncategorized | Status: assigned
Component: contrib.auth | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by fcurella):

* owner: nobody => fcurella
* status: new => assigned


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

Django

unread,
Jul 20, 2015, 1:12:45 PM7/20/15
to django-...@googlegroups.com
#25148: Add a warning for `CASCADE`ing foreign keys on custom User models.
-------------------------------+--------------------------------------
Reporter: fcurella | Owner: fcurella
Type: Uncategorized | Status: assigned
Component: contrib.auth | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by timgraham):

A better use of your time may be to fix #21127 -- changing the default of
`on_delete`. At that point, I don't think such a check would be needed.

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

Django

unread,
Jul 20, 2015, 1:26:28 PM7/20/15
to django-...@googlegroups.com
#25148: Add a warning for `CASCADE`ing foreign keys on custom User models.
-------------------------------+--------------------------------------
Reporter: fcurella | Owner: fcurella
Type: Uncategorized | Status: assigned
Component: contrib.auth | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by fcurella):

Thank Tim, I'll see if I can get consensus on that ticket.

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

Django

unread,
Jul 20, 2015, 1:26:47 PM7/20/15
to django-...@googlegroups.com
#25148: Add a warning for `CASCADE`ing foreign keys on custom User models.
-------------------------------+--------------------------------------
Reporter: fcurella | Owner: fcurella
Type: Uncategorized | Status: closed
Component: contrib.auth | Version: 1.8
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by fcurella):

* status: assigned => closed
* resolution: => duplicate


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

Reply all
Reply to author
Forward
0 new messages