Skip to content

Commit

Permalink
django-2.0: Add on_delete on ForeignKeys.
Browse files Browse the repository at this point in the history
In Django 2.0, one must specify the on_delete behavior for all
ForeignKeys explicitly.
  • Loading branch information
rht authored and timabbott committed Jan 30, 2018
1 parent 8e268b5 commit 8106a25
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 12 deletions.
36 changes: 36 additions & 0 deletions analytics/migrations/0012_add_on_delete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2018-01-29 08:14
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('analytics', '0011_clear_analytics_tables'),
]

operations = [
migrations.AlterField(
model_name='installationcount',
name='anomaly',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='analytics.Anomaly'),
),
migrations.AlterField(
model_name='realmcount',
name='anomaly',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='analytics.Anomaly'),
),
migrations.AlterField(
model_name='streamcount',
name='anomaly',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='analytics.Anomaly'),
),
migrations.AlterField(
model_name='usercount',
name='anomaly',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='analytics.Anomaly'),
),
]
12 changes: 6 additions & 6 deletions analytics/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class BaseCount(models.Model):
subgroup = models.CharField(max_length=16, null=True) # type: Optional[Text]
end_time = models.DateTimeField() # type: datetime.datetime
value = models.BigIntegerField() # type: int
anomaly = models.ForeignKey(Anomaly, null=True) # type: Optional[Anomaly]
anomaly = models.ForeignKey(Anomaly, on_delete=models.SET_NULL, null=True) # type: Optional[Anomaly]

class Meta:
abstract = True
Expand All @@ -63,7 +63,7 @@ def __str__(self) -> Text:
return "<InstallationCount: %s %s %s>" % (self.property, self.subgroup, self.value)

class RealmCount(BaseCount):
realm = models.ForeignKey(Realm)
realm = models.ForeignKey(Realm, on_delete=models.CASCADE)

class Meta:
unique_together = ("realm", "property", "subgroup", "end_time")
Expand All @@ -73,8 +73,8 @@ def __str__(self) -> Text:
return "<RealmCount: %s %s %s %s>" % (self.realm, self.property, self.subgroup, self.value)

class UserCount(BaseCount):
user = models.ForeignKey(UserProfile)
realm = models.ForeignKey(Realm)
user = models.ForeignKey(UserProfile, on_delete=models.CASCADE)
realm = models.ForeignKey(Realm, on_delete=models.CASCADE)

class Meta:
unique_together = ("user", "property", "subgroup", "end_time")
Expand All @@ -86,8 +86,8 @@ def __str__(self) -> Text:
return "<UserCount: %s %s %s %s>" % (self.user, self.property, self.subgroup, self.value)

class StreamCount(BaseCount):
stream = models.ForeignKey(Stream)
realm = models.ForeignKey(Realm)
stream = models.ForeignKey(Stream, on_delete=models.CASCADE)
realm = models.ForeignKey(Realm, on_delete=models.CASCADE)

class Meta:
unique_together = ("stream", "property", "subgroup", "end_time")
Expand Down
4 changes: 2 additions & 2 deletions confirmation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from django.db import models
from django.db.models import CASCADE
from django.core.urlresolvers import reverse
from django.urls import reverse
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey
Expand Down Expand Up @@ -85,7 +85,7 @@ def confirmation_url(confirmation_key: str, host: str,
reverse(_properties[confirmation_type].url_name, kwargs=url_args))

class Confirmation(models.Model):
content_type = models.ForeignKey(ContentType)
content_type = models.ForeignKey(ContentType, on_delete=CASCADE)
object_id = models.PositiveIntegerField() # type: int
content_object = GenericForeignKey('content_type', 'object_id')
date_sent = models.DateTimeField() # type: datetime.datetime
Expand Down
6 changes: 3 additions & 3 deletions zerver/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -756,15 +756,15 @@ def major_tos_version(self) -> int:
class UserGroup(models.Model):
name = models.CharField(max_length=100)
members = models.ManyToManyField(UserProfile, through='UserGroupMembership')
realm = models.ForeignKey(Realm)
realm = models.ForeignKey(Realm, on_delete=CASCADE)
description = models.CharField(max_length=1024, default=u'') # type: Text

class Meta:
unique_together = (('realm', 'name'),)

class UserGroupMembership(models.Model):
user_group = models.ForeignKey(UserGroup)
user_profile = models.ForeignKey(UserProfile)
user_group = models.ForeignKey(UserGroup, on_delete=CASCADE)
user_profile = models.ForeignKey(UserProfile, on_delete=CASCADE)

class Meta:
unique_together = (('user_group', 'user_profile'),)
Expand Down
2 changes: 1 addition & 1 deletion zilencer/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class RemoteZulipServer(models.Model):

# Variant of PushDeviceToken for a remote server.
class RemotePushDeviceToken(AbstractPushDeviceToken):
server = models.ForeignKey(RemoteZulipServer) # type: RemoteZulipServer
server = models.ForeignKey(RemoteZulipServer, on_delete=models.CASCADE) # type: RemoteZulipServer
# The user id on the remote server for this device device this is
user_id = models.BigIntegerField(db_index=True) # type: int
token = models.CharField(max_length=4096, db_index=True) # type: bytes
Expand Down

0 comments on commit 8106a25

Please sign in to comment.