From f104c09424f9c6b2557db6ef08f40893ee4ea2ed Mon Sep 17 00:00:00 2001 From: John Whitlock Date: Sun, 27 Aug 2017 11:52:56 -0500 Subject: [PATCH] Add on_delete arguments to ForeignKeys This attribute will be required in Django 2.0. Update initial migration to include the default of CASCADE, and then use better choices for applicable models. --- multigtfs/migrations/0001_initial.py | 56 +++++++++---------- multigtfs/migrations/0002_add_on_delete.py | 65 ++++++++++++++++++++++ multigtfs/models/agency.py | 2 +- multigtfs/models/block.py | 2 +- multigtfs/models/fare.py | 2 +- multigtfs/models/fare_rule.py | 10 ++-- multigtfs/models/feed_info.py | 2 +- multigtfs/models/frequency.py | 2 +- multigtfs/models/route.py | 5 +- multigtfs/models/service.py | 2 +- multigtfs/models/service_date.py | 2 +- multigtfs/models/shape.py | 5 +- multigtfs/models/stop.py | 6 +- multigtfs/models/stop_time.py | 4 +- multigtfs/models/transfer.py | 4 +- multigtfs/models/trip.py | 9 +-- multigtfs/models/zone.py | 2 +- 17 files changed, 124 insertions(+), 56 deletions(-) create mode 100644 multigtfs/migrations/0002_add_on_delete.py diff --git a/multigtfs/migrations/0001_initial.py b/multigtfs/migrations/0001_initial.py index ea7b2b7..8ce5dc0 100644 --- a/multigtfs/migrations/0001_initial.py +++ b/multigtfs/migrations/0001_initial.py @@ -96,7 +96,7 @@ class Migration(migrations.Migration): ('end_date', models.DateField(help_text='Date that feed stops providing reliable data.', null=True, blank=True)), ('version', models.CharField(help_text='Version of feed.', max_length=255, blank=True)), ('extra_data', jsonfield.fields.JSONField(default={}, null=True, blank=True)), - ('feed', models.ForeignKey(to='multigtfs.Feed')), + ('feed', models.ForeignKey(to='multigtfs.Feed', on_delete=django.db.models.deletion.CASCADE)), ], options={ 'db_table': 'feed_info', @@ -134,8 +134,8 @@ class Migration(migrations.Migration): ('text_color', models.CharField(help_text='Color of route text in hex', max_length=6, blank=True)), ('geometry', django.contrib.gis.db.models.fields.MultiLineStringField(help_text='Geometry cache of Trips', srid=4326, null=True, blank=True)), ('extra_data', jsonfield.fields.JSONField(default={}, null=True, blank=True)), - ('agency', models.ForeignKey(blank=True, to='multigtfs.Agency', help_text='Agency for this route.', null=True)), - ('feed', models.ForeignKey(to='multigtfs.Feed')), + ('agency', models.ForeignKey(blank=True, to='multigtfs.Agency', help_text='Agency for this route.', null=True, on_delete=django.db.models.deletion.CASCADE)), + ('feed', models.ForeignKey(to='multigtfs.Feed', on_delete=django.db.models.deletion.CASCADE)), ], options={ 'db_table': 'route', @@ -157,7 +157,7 @@ class Migration(migrations.Migration): ('start_date', models.DateField(null=True, blank=True)), ('end_date', models.DateField(null=True, blank=True)), ('extra_data', jsonfield.fields.JSONField(default={}, null=True, blank=True)), - ('feed', models.ForeignKey(to='multigtfs.Feed')), + ('feed', models.ForeignKey(to='multigtfs.Feed', on_delete=django.db.models.deletion.CASCADE)), ], options={ 'db_table': 'service', @@ -171,7 +171,7 @@ class Migration(migrations.Migration): ('date', models.DateField(help_text='Date that the service differs from the norm.')), ('exception_type', models.IntegerField(default=1, help_text='Is service added or removed on this date?', choices=[(1, 'Added'), (2, 'Removed')])), ('extra_data', jsonfield.fields.JSONField(default={}, null=True, blank=True)), - ('service', models.ForeignKey(to='multigtfs.Service')), + ('service', models.ForeignKey(to='multigtfs.Service', on_delete=django.db.models.deletion.CASCADE)), ], options={ 'db_table': 'service_date', @@ -184,7 +184,7 @@ class Migration(migrations.Migration): ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), ('shape_id', models.CharField(help_text='Unique identifier for a shape.', max_length=255, db_index=True)), ('geometry', django.contrib.gis.db.models.fields.LineStringField(help_text='Geometry cache of ShapePoints', srid=4326, null=True, blank=True)), - ('feed', models.ForeignKey(to='multigtfs.Feed')), + ('feed', models.ForeignKey(to='multigtfs.Feed', on_delete=django.db.models.deletion.CASCADE)), ], options={ 'db_table': 'shape', @@ -199,7 +199,7 @@ class Migration(migrations.Migration): ('sequence', models.IntegerField()), ('traveled', models.FloatField(help_text='Distance of point from start of shape', null=True, blank=True)), ('extra_data', jsonfield.fields.JSONField(default={}, null=True, blank=True)), - ('shape', models.ForeignKey(related_name='points', to='multigtfs.Shape')), + ('shape', models.ForeignKey(related_name='points', to='multigtfs.Shape', on_delete=django.db.models.deletion.CASCADE)), ], options={ 'db_table': 'shape_point', @@ -220,8 +220,8 @@ class Migration(migrations.Migration): ('timezone', models.CharField(help_text='Timezone of the stop', max_length=255, blank=True)), ('wheelchair_boarding', models.CharField(blank=True, help_text='Is wheelchair boarding possible?', max_length=1, choices=[('0', 'No information'), ('1', 'Some wheelchair boarding'), ('2', 'No wheelchair boarding')])), ('extra_data', jsonfield.fields.JSONField(default={}, null=True, blank=True)), - ('feed', models.ForeignKey(to='multigtfs.Feed')), - ('parent_station', models.ForeignKey(blank=True, to='multigtfs.Stop', help_text='The station associated with the stop', null=True)), + ('feed', models.ForeignKey(to='multigtfs.Feed', on_delete=django.db.models.deletion.CASCADE)), + ('parent_station', models.ForeignKey(blank=True, to='multigtfs.Stop', help_text='The station associated with the stop', null=True, on_delete=django.db.models.deletion.CASCADE)), ], options={ 'db_table': 'stop', @@ -240,7 +240,7 @@ class Migration(migrations.Migration): ('drop_off_type', models.CharField(blank=True, help_text='How passengers are picked up', max_length=1, choices=[('0', 'Regularly scheduled drop off'), ('1', 'No drop off available'), ('2', 'Must phone agency to arrange drop off'), ('3', 'Must coordinate with driver to arrange drop off')])), ('shape_dist_traveled', models.FloatField(help_text='Distance of stop from start of shape', null=True, verbose_name='shape distance traveled', blank=True)), ('extra_data', jsonfield.fields.JSONField(default={}, null=True, blank=True)), - ('stop', models.ForeignKey(to='multigtfs.Stop')), + ('stop', models.ForeignKey(to='multigtfs.Stop', on_delete=django.db.models.deletion.CASCADE)), ], options={ 'db_table': 'stop_time', @@ -254,8 +254,8 @@ class Migration(migrations.Migration): ('transfer_type', models.IntegerField(default=0, help_text='What kind of transfer?', blank=True, choices=[(0, 'Recommended transfer point'), (1, 'Timed transfer point (vehicle will wait)'), (2, 'min_transfer_time needed to successfully transfer'), (3, 'No transfers possible')])), ('min_transfer_time', models.IntegerField(help_text='How many seconds are required to transfer?', null=True, blank=True)), ('extra_data', jsonfield.fields.JSONField(default={}, null=True, blank=True)), - ('from_stop', models.ForeignKey(related_name='transfer_from_stop', to='multigtfs.Stop', help_text='Stop where a connection between routes begins.')), - ('to_stop', models.ForeignKey(related_name='transfer_to_stop', to='multigtfs.Stop', help_text='Stop where a connection between routes ends.')), + ('from_stop', models.ForeignKey(related_name='transfer_from_stop', to='multigtfs.Stop', help_text='Stop where a connection between routes begins.', on_delete=django.db.models.deletion.CASCADE)), + ('to_stop', models.ForeignKey(related_name='transfer_to_stop', to='multigtfs.Stop', help_text='Stop where a connection between routes ends.', on_delete=django.db.models.deletion.CASCADE)), ], options={ 'db_table': 'transfer', @@ -274,10 +274,10 @@ class Migration(migrations.Migration): ('wheelchair_accessible', models.CharField(blank=True, help_text='Are there accommodations for riders with wheelchair?', max_length=1, choices=[('0', 'No information'), ('1', 'Some wheelchair accommodation'), ('2', 'No wheelchair accommodation')])), ('bikes_allowed', models.CharField(blank=True, help_text='Are bicycles allowed?', max_length=1, choices=[('0', 'No information'), ('1', 'Some bicycle accommodation'), ('2', 'No bicycles allowed')])), ('extra_data', jsonfield.fields.JSONField(default={}, null=True, blank=True)), - ('block', models.ForeignKey(blank=True, to='multigtfs.Block', help_text='Block of sequential trips that this trip belongs to.', null=True)), - ('route', models.ForeignKey(to='multigtfs.Route')), - ('service', models.ForeignKey(blank=True, to='multigtfs.Service', null=True)), - ('shape', models.ForeignKey(blank=True, to='multigtfs.Shape', help_text='Shape used for this trip', null=True)), + ('block', models.ForeignKey(blank=True, to='multigtfs.Block', help_text='Block of sequential trips that this trip belongs to.', null=True, on_delete=django.db.models.deletion.CASCADE)), + ('route', models.ForeignKey(to='multigtfs.Route', on_delete=django.db.models.deletion.CASCADE)), + ('service', models.ForeignKey(blank=True, to='multigtfs.Service', null=True, on_delete=django.db.models.deletion.CASCADE)), + ('shape', models.ForeignKey(blank=True, to='multigtfs.Shape', help_text='Shape used for this trip', null=True, on_delete=django.db.models.deletion.CASCADE)), ], options={ 'db_table': 'trip', @@ -289,7 +289,7 @@ class Migration(migrations.Migration): fields=[ ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), ('zone_id', models.CharField(help_text='Unique identifier for a zone.', max_length=63, db_index=True)), - ('feed', models.ForeignKey(to='multigtfs.Feed')), + ('feed', models.ForeignKey(to='multigtfs.Feed', on_delete=django.db.models.deletion.CASCADE)), ], options={ 'db_table': 'zone', @@ -299,67 +299,67 @@ class Migration(migrations.Migration): migrations.AddField( model_name='stoptime', name='trip', - field=models.ForeignKey(to='multigtfs.Trip'), + field=models.ForeignKey(to='multigtfs.Trip', on_delete=django.db.models.deletion.CASCADE), preserve_default=True, ), migrations.AddField( model_name='stop', name='zone', - field=models.ForeignKey(blank=True, to='multigtfs.Zone', help_text='Fare zone for a stop ID.', null=True), + field=models.ForeignKey(blank=True, to='multigtfs.Zone', help_text='Fare zone for a stop ID.', null=True, on_delete=django.db.models.deletion.CASCADE), preserve_default=True, ), migrations.AddField( model_name='frequency', name='trip', - field=models.ForeignKey(to='multigtfs.Trip'), + field=models.ForeignKey(to='multigtfs.Trip', on_delete=django.db.models.deletion.CASCADE), preserve_default=True, ), migrations.AddField( model_name='farerule', name='contains', - field=models.ForeignKey(related_name='fare_contains', blank=True, to='multigtfs.Zone', help_text='Fare class is valid for travel withing this zone.', null=True), + field=models.ForeignKey(related_name='fare_contains', blank=True, to='multigtfs.Zone', help_text='Fare class is valid for travel withing this zone.', null=True, on_delete=django.db.models.deletion.CASCADE), preserve_default=True, ), migrations.AddField( model_name='farerule', name='destination', - field=models.ForeignKey(related_name='fare_destinations', blank=True, to='multigtfs.Zone', help_text='Fare class is valid for travel ending in this zone.', null=True), + field=models.ForeignKey(related_name='fare_destinations', blank=True, to='multigtfs.Zone', help_text='Fare class is valid for travel ending in this zone.', null=True, on_delete=django.db.models.deletion.CASCADE), preserve_default=True, ), migrations.AddField( model_name='farerule', name='fare', - field=models.ForeignKey(to='multigtfs.Fare'), + field=models.ForeignKey(to='multigtfs.Fare', on_delete=django.db.models.deletion.CASCADE), preserve_default=True, ), migrations.AddField( model_name='farerule', name='origin', - field=models.ForeignKey(related_name='fare_origins', blank=True, to='multigtfs.Zone', help_text='Fare class is valid for travel originating in this zone.', null=True), + field=models.ForeignKey(related_name='fare_origins', blank=True, to='multigtfs.Zone', help_text='Fare class is valid for travel originating in this zone.', null=True, on_delete=django.db.models.deletion.CASCADE), preserve_default=True, ), migrations.AddField( model_name='farerule', name='route', - field=models.ForeignKey(blank=True, to='multigtfs.Route', help_text='Fare class is valid for this route.', null=True), + field=models.ForeignKey(blank=True, to='multigtfs.Route', help_text='Fare class is valid for this route.', null=True, on_delete=django.db.models.deletion.CASCADE), preserve_default=True, ), migrations.AddField( model_name='fare', name='feed', - field=models.ForeignKey(to='multigtfs.Feed'), + field=models.ForeignKey(to='multigtfs.Feed', on_delete=django.db.models.deletion.CASCADE), preserve_default=True, ), migrations.AddField( model_name='block', name='feed', - field=models.ForeignKey(to='multigtfs.Feed'), + field=models.ForeignKey(to='multigtfs.Feed', on_delete=django.db.models.deletion.CASCADE), preserve_default=True, ), migrations.AddField( model_name='agency', name='feed', - field=models.ForeignKey(to='multigtfs.Feed'), + field=models.ForeignKey(to='multigtfs.Feed', on_delete=django.db.models.deletion.CASCADE), preserve_default=True, ), ] diff --git a/multigtfs/migrations/0002_add_on_delete.py b/multigtfs/migrations/0002_add_on_delete.py new file mode 100644 index 0000000..254e4b9 --- /dev/null +++ b/multigtfs/migrations/0002_add_on_delete.py @@ -0,0 +1,65 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('multigtfs', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='farerule', + name='contains', + field=models.ForeignKey(blank=True, null=True, help_text='Fare class is valid for travel withing this zone.', related_name='fare_contains', on_delete=django.db.models.deletion.SET_NULL, to='multigtfs.Zone'), + ), + migrations.AlterField( + model_name='farerule', + name='destination', + field=models.ForeignKey(blank=True, null=True, help_text='Fare class is valid for travel ending in this zone.', related_name='fare_destinations', on_delete=django.db.models.deletion.SET_NULL, to='multigtfs.Zone'), + ), + migrations.AlterField( + model_name='farerule', + name='origin', + field=models.ForeignKey(blank=True, null=True, help_text='Fare class is valid for travel originating in this zone.', related_name='fare_origins', on_delete=django.db.models.deletion.SET_NULL, to='multigtfs.Zone'), + ), + migrations.AlterField( + model_name='farerule', + name='route', + field=models.ForeignKey(blank=True, null=True, help_text='Fare class is valid for this route.', on_delete=django.db.models.deletion.SET_NULL, to='multigtfs.Route'), + ), + migrations.AlterField( + model_name='route', + name='agency', + field=models.ForeignKey(blank=True, null=True, help_text='Agency for this route.', on_delete=django.db.models.deletion.SET_NULL, to='multigtfs.Agency'), + ), + migrations.AlterField( + model_name='stop', + name='parent_station', + field=models.ForeignKey(blank=True, null=True, help_text='The station associated with the stop', on_delete=django.db.models.deletion.SET_NULL, to='multigtfs.Stop'), + ), + migrations.AlterField( + model_name='stop', + name='zone', + field=models.ForeignKey(blank=True, null=True, help_text='Fare zone for a stop ID.', on_delete=django.db.models.deletion.SET_NULL, to='multigtfs.Zone'), + ), + migrations.AlterField( + model_name='trip', + name='block', + field=models.ForeignKey(blank=True, null=True, help_text='Block of sequential trips that this trip belongs to.', on_delete=django.db.models.deletion.SET_NULL, to='multigtfs.Block'), + ), + migrations.AlterField( + model_name='trip', + name='service', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='multigtfs.Service'), + ), + migrations.AlterField( + model_name='trip', + name='shape', + field=models.ForeignKey(blank=True, null=True, help_text='Shape used for this trip', on_delete=django.db.models.deletion.SET_NULL, to='multigtfs.Shape'), + ), + ] diff --git a/multigtfs/models/agency.py b/multigtfs/models/agency.py index df57204..4c1f833 100644 --- a/multigtfs/models/agency.py +++ b/multigtfs/models/agency.py @@ -27,7 +27,7 @@ class Agency(Base): Maps to agency.txt in the GTFS feed. """ - feed = models.ForeignKey('Feed') + feed = models.ForeignKey('Feed', on_delete=models.CASCADE) agency_id = models.CharField( max_length=255, blank=True, db_index=True, help_text="Unique identifier for transit agency") diff --git a/multigtfs/models/block.py b/multigtfs/models/block.py index 9f33ed7..da9f8b0 100644 --- a/multigtfs/models/block.py +++ b/multigtfs/models/block.py @@ -27,7 +27,7 @@ class Block(Base): This data is not represented as a file in the GTFS. It appears as an identifier in the trip table. """ - feed = models.ForeignKey('Feed') + feed = models.ForeignKey('Feed', on_delete=models.CASCADE) block_id = models.CharField( max_length=63, db_index=True, help_text="Unique identifier for a block.") diff --git a/multigtfs/models/fare.py b/multigtfs/models/fare.py index e90f1cf..638d999 100644 --- a/multigtfs/models/fare.py +++ b/multigtfs/models/fare.py @@ -24,7 +24,7 @@ class Fare(Base): """A fare class""" - feed = models.ForeignKey('Feed') + feed = models.ForeignKey('Feed', on_delete=models.CASCADE) fare_id = models.CharField( max_length=255, db_index=True, help_text="Unique identifier for a fare class") diff --git a/multigtfs/models/fare_rule.py b/multigtfs/models/fare_rule.py index 80c3966..2bdf38f 100644 --- a/multigtfs/models/fare_rule.py +++ b/multigtfs/models/fare_rule.py @@ -23,20 +23,20 @@ @python_2_unicode_compatible class FareRule(Base): """Associate a Fare with a Route and/or Zones""" - fare = models.ForeignKey('Fare') + fare = models.ForeignKey('Fare', on_delete=models.CASCADE) route = models.ForeignKey( - 'Route', null=True, blank=True, + 'Route', null=True, blank=True, on_delete=models.SET_NULL, help_text="Fare class is valid for this route.") origin = models.ForeignKey( - 'Zone', null=True, blank=True, + 'Zone', null=True, blank=True, on_delete=models.SET_NULL, related_name='fare_origins', help_text="Fare class is valid for travel originating in this zone.") destination = models.ForeignKey( - 'Zone', null=True, blank=True, + 'Zone', null=True, blank=True, on_delete=models.SET_NULL, related_name='fare_destinations', help_text="Fare class is valid for travel ending in this zone.") contains = models.ForeignKey( - 'Zone', null=True, blank=True, + 'Zone', null=True, blank=True, on_delete=models.SET_NULL, related_name='fare_contains', help_text="Fare class is valid for travel withing this zone.") extra_data = JSONField(default={}, blank=True, null=True) diff --git a/multigtfs/models/feed_info.py b/multigtfs/models/feed_info.py index 529f184..123490c 100644 --- a/multigtfs/models/feed_info.py +++ b/multigtfs/models/feed_info.py @@ -26,7 +26,7 @@ class FeedInfo(Base): Implements feed_info.txt in the GTFS feed. """ - feed = models.ForeignKey('Feed') + feed = models.ForeignKey('Feed', on_delete=models.CASCADE) publisher_name = models.CharField( max_length=255, help_text="Full name of organization that publishes the feed.") diff --git a/multigtfs/models/frequency.py b/multigtfs/models/frequency.py index 744b8aa..3116825 100644 --- a/multigtfs/models/frequency.py +++ b/multigtfs/models/frequency.py @@ -24,7 +24,7 @@ @python_2_unicode_compatible class Frequency(Base): """Description of a trip that repeats without fixed stop times""" - trip = models.ForeignKey('Trip') + trip = models.ForeignKey('Trip', on_delete=models.CASCADE) start_time = SecondsField( help_text="Time that the service begins at the specified frequency") end_time = SecondsField( diff --git a/multigtfs/models/route.py b/multigtfs/models/route.py index 97b70a8..ec0b767 100644 --- a/multigtfs/models/route.py +++ b/multigtfs/models/route.py @@ -28,12 +28,13 @@ class Route(Base): Maps to route.txt in the GTFS feed. """ - feed = models.ForeignKey('Feed') + feed = models.ForeignKey('Feed', on_delete=models.CASCADE) route_id = models.CharField( max_length=255, db_index=True, help_text="Unique identifier for route.") agency = models.ForeignKey( - 'Agency', null=True, blank=True, help_text="Agency for this route.") + 'Agency', null=True, blank=True, on_delete=models.SET_NULL, + help_text="Agency for this route.") short_name = models.CharField( max_length=63, help_text="Short name of the route") diff --git a/multigtfs/models/service.py b/multigtfs/models/service.py index 256b253..1ad76fc 100644 --- a/multigtfs/models/service.py +++ b/multigtfs/models/service.py @@ -27,7 +27,7 @@ class Service(Base): Implements calendar.txt """ - feed = models.ForeignKey('Feed') + feed = models.ForeignKey('Feed', on_delete=models.CASCADE) service_id = models.CharField( max_length=255, db_index=True, help_text="Unique identifier for service dates.") diff --git a/multigtfs/models/service_date.py b/multigtfs/models/service_date.py index 4b12600..b53eed6 100644 --- a/multigtfs/models/service_date.py +++ b/multigtfs/models/service_date.py @@ -26,7 +26,7 @@ class ServiceDate(Base): Implements calendar_dates.txt """ - service = models.ForeignKey('Service') + service = models.ForeignKey('Service', on_delete=models.CASCADE) date = models.DateField( help_text="Date that the service differs from the norm.") exception_type = models.IntegerField( diff --git a/multigtfs/models/shape.py b/multigtfs/models/shape.py index 9421732..dc77040 100644 --- a/multigtfs/models/shape.py +++ b/multigtfs/models/shape.py @@ -29,7 +29,7 @@ class Shape(Base): """The path the vehicle takes along the route. Implements shapes.txt.""" - feed = models.ForeignKey('Feed') + feed = models.ForeignKey('Feed', on_delete=models.CASCADE) shape_id = models.CharField( max_length=255, db_index=True, help_text="Unique identifier for a shape.") @@ -63,7 +63,8 @@ class Meta: @python_2_unicode_compatible class ShapePoint(Base): """A point along the shape""" - shape = models.ForeignKey('Shape', related_name='points') + shape = models.ForeignKey( + 'Shape', on_delete=models.CASCADE, related_name='points') point = models.PointField( help_text='WGS 84 latitude/longitude of shape point') sequence = models.IntegerField() diff --git a/multigtfs/models/stop.py b/multigtfs/models/stop.py index aba32a8..3609f5f 100644 --- a/multigtfs/models/stop.py +++ b/multigtfs/models/stop.py @@ -34,7 +34,7 @@ class Stop(Base): Maps to stops.txt in the GTFS feed. """ - feed = models.ForeignKey('Feed') + feed = models.ForeignKey('Feed', on_delete=models.CASCADE) stop_id = models.CharField( max_length=255, db_index=True, help_text="Unique identifier for a stop or station.") @@ -51,7 +51,7 @@ class Stop(Base): point = models.PointField( help_text='WGS 84 latitude/longitude of stop or station') zone = models.ForeignKey( - 'Zone', null=True, blank=True, + 'Zone', null=True, blank=True, on_delete=models.SET_NULL, help_text="Fare zone for a stop ID.") url = models.URLField( blank=True, help_text="URL for the stop") @@ -59,7 +59,7 @@ class Stop(Base): max_length=1, blank=True, choices=(('0', 'Stop'), ('1', 'Station')), help_text="Is this a stop or station?") parent_station = models.ForeignKey( - 'Stop', null=True, blank=True, + 'Stop', null=True, blank=True, on_delete=models.SET_NULL, help_text="The station associated with the stop") timezone = models.CharField( max_length=255, blank=True, diff --git a/multigtfs/models/stop_time.py b/multigtfs/models/stop_time.py index 2b7e54c..9ad8525 100644 --- a/multigtfs/models/stop_time.py +++ b/multigtfs/models/stop_time.py @@ -29,8 +29,8 @@ class StopTime(Base): This implements stop_times.txt in the GTFS feed """ - trip = models.ForeignKey(Trip) - stop = models.ForeignKey(Stop) + trip = models.ForeignKey(Trip, on_delete=models.CASCADE) + stop = models.ForeignKey(Stop, on_delete=models.CASCADE) arrival_time = SecondsField( default=None, null=True, blank=True, help_text="Arrival time. Must be set for end stops of trip.") diff --git a/multigtfs/models/transfer.py b/multigtfs/models/transfer.py index 69ed345..fe7ceb2 100644 --- a/multigtfs/models/transfer.py +++ b/multigtfs/models/transfer.py @@ -27,11 +27,11 @@ class Transfer(Base): Implements transfer.txt in the GTFS feed. """ from_stop = models.ForeignKey( - 'Stop', + 'Stop', on_delete=models.CASCADE, related_name='transfer_from_stop', help_text='Stop where a connection between routes begins.') to_stop = models.ForeignKey( - 'Stop', + 'Stop', on_delete=models.CASCADE, related_name='transfer_to_stop', help_text='Stop where a connection between routes ends.') transfer_type = models.IntegerField( diff --git a/multigtfs/models/trip.py b/multigtfs/models/trip.py index deceec8..12c71ad 100644 --- a/multigtfs/models/trip.py +++ b/multigtfs/models/trip.py @@ -28,8 +28,9 @@ class Trip(Base): This implements trips.txt in the GTFS feed """ - route = models.ForeignKey('Route') - service = models.ForeignKey('Service', null=True, blank=True) + route = models.ForeignKey('Route', on_delete=models.CASCADE) + service = models.ForeignKey( + 'Service', null=True, blank=True, on_delete=models.SET_NULL) trip_id = models.CharField( max_length=255, db_index=True, help_text="Unique identifier for a trip.") @@ -44,10 +45,10 @@ class Trip(Base): choices=(('0', '0'), ('1', '1')), help_text="Direction for bi-directional routes.") block = models.ForeignKey( - 'Block', null=True, blank=True, + 'Block', null=True, blank=True, on_delete=models.SET_NULL, help_text="Block of sequential trips that this trip belongs to.") shape = models.ForeignKey( - 'Shape', null=True, blank=True, + 'Shape', null=True, blank=True, on_delete=models.SET_NULL, help_text="Shape used for this trip") geometry = models.LineStringField( null=True, blank=True, diff --git a/multigtfs/models/zone.py b/multigtfs/models/zone.py index a5517db..e132db8 100644 --- a/multigtfs/models/zone.py +++ b/multigtfs/models/zone.py @@ -26,7 +26,7 @@ class Zone(Base): This data is not represented as a file in the GTFS. It appears as an identifier in the fare_rules and the stop tables. """ - feed = models.ForeignKey('Feed') + feed = models.ForeignKey('Feed', on_delete=models.CASCADE) zone_id = models.CharField( max_length=63, db_index=True, help_text="Unique identifier for a zone.")