From 8106a25e61d09f1cd9bfb50e2e620329db993cf3 Mon Sep 17 00:00:00 2001 From: rht Date: Mon, 29 Jan 2018 12:17:31 +0500 Subject: [PATCH] django-2.0: Add on_delete on ForeignKeys. In Django 2.0, one must specify the on_delete behavior for all ForeignKeys explicitly. --- analytics/migrations/0012_add_on_delete.py | 36 ++++++++++++++++++++++ analytics/models.py | 12 ++++---- confirmation/models.py | 4 +-- zerver/models.py | 6 ++-- zilencer/models.py | 2 +- 5 files changed, 48 insertions(+), 12 deletions(-) create mode 100644 analytics/migrations/0012_add_on_delete.py diff --git a/analytics/migrations/0012_add_on_delete.py b/analytics/migrations/0012_add_on_delete.py new file mode 100644 index 0000000000000..ee298af86070e --- /dev/null +++ b/analytics/migrations/0012_add_on_delete.py @@ -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'), + ), + ] diff --git a/analytics/models.py b/analytics/models.py index ef12844482404..6a5b097a17fc5 100644 --- a/analytics/models.py +++ b/analytics/models.py @@ -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 @@ -63,7 +63,7 @@ def __str__(self) -> Text: return "" % (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") @@ -73,8 +73,8 @@ def __str__(self) -> Text: return "" % (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") @@ -86,8 +86,8 @@ def __str__(self) -> Text: return "" % (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") diff --git a/confirmation/models.py b/confirmation/models.py index 157986a4ebd1b..076a17760b631 100644 --- a/confirmation/models.py +++ b/confirmation/models.py @@ -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 @@ -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 diff --git a/zerver/models.py b/zerver/models.py index d469721e316f6..80602b764a344 100644 --- a/zerver/models.py +++ b/zerver/models.py @@ -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'),) diff --git a/zilencer/models.py b/zilencer/models.py index 79c2ab5d8f563..72eb794149d2d 100644 --- a/zilencer/models.py +++ b/zilencer/models.py @@ -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