Skip to content

Commit

Permalink
Add on_delete arguments to ForeignKeys
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jwhitlock committed Aug 27, 2017
1 parent 78793e5 commit f104c09
Show file tree
Hide file tree
Showing 17 changed files with 124 additions and 56 deletions.
56 changes: 28 additions & 28 deletions multigtfs/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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,
),
]
65 changes: 65 additions & 0 deletions multigtfs/migrations/0002_add_on_delete.py
Original file line number Diff line number Diff line change
@@ -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'),
),
]
2 changes: 1 addition & 1 deletion multigtfs/models/agency.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion multigtfs/models/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
2 changes: 1 addition & 1 deletion multigtfs/models/fare.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
10 changes: 5 additions & 5 deletions multigtfs/models/fare_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion multigtfs/models/feed_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
Loading

0 comments on commit f104c09

Please sign in to comment.