Opened 7 years ago

Closed 7 years ago

#28677 closed Bug (invalid)

on_delete is a required positional argument for ForeignKeys, even in migrations

Reported by: Christian Kreuzberger Owned by: nobody
Component: Migrations Version: 2.0
Severity: Normal Keywords: migration foreignkey on_delete
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hi!

With version 2.0 you added on_delete as a required argument for ForeignKeys. This seems to also be true for existing migrations of pre-2.0 projects.
For instance, I attached a migration that I created last year with (i think) Django 1.8. This migration now fails with the following error (e.g., when running python manage.py check):

  File "/home/user/projects/user_foreign_key_testapp/polls/migrations/0005_auto_20160414_1351.py", line 7, in <module>
    class Migration(migrations.Migration):
  File "/home/user/projects/user_foreign_key_testapp/polls/migrations/0005_auto_20160414_1351.py", line 17, in Migration
    field=models.ForeignKey(related_name='choices', verbose_name=b'Which poll?', to='polls.Poll'),
TypeError: __init__() missing 1 required positional argument: 'on_delete'

I believe that I should not have to change all my existing migrations for all my projects, and therefore on_delete should not be required for those migrations.

Attachments (1)

0005_auto_20160414_1351.py (446 bytes ) - added by Christian Kreuzberger 7 years ago.

Download all attachments as: .zip

Change History (7)

by Christian Kreuzberger, 7 years ago

Attachment: 0005_auto_20160414_1351.py added

comment:1 by Tim Graham, 7 years ago

Resolution: invalid
Status: newclosed

Yes, it's required in migrations as documented. Consider squashing migrations so you don't have many to update.

comment:2 by Tim Graham <timograham@…>, 7 years ago

In 491eb56:

Refs #28677 -- Doc'd that on_delete is required in migrations.

comment:3 by Tim Graham <timograham@…>, 7 years ago

In cfde4ac3:

[2.0.x] Refs #28677 -- Doc'd that on_delete is required in migrations.

Backport of 491eb56fd4dea10248ccced90c31ed64152d09cb from master

in reply to:  3 comment:4 by Christian Kreuzberger, 7 years ago

Replying to Tim Graham <timograham@…>:

In cfde4ac3:

[2.0.x] Refs #28677 -- Doc'd that on_delete is required in migrations.

Backport of 491eb56fd4dea10248ccced90c31ed64152d09cb from master

Thanks for clarifying, and thank you for adding this to the documentation!

comment:5 by Christian Kreuzberger, 7 years ago

Resolution: invalid
Status: closednew

Sorry if re-opening this ticket is against the rules, but I just tried squashing the migrations and I ran into the same problem.

I squashed the migrations with Django 1.11 and then upgraded to Django 2.0a1. Here are my migrations:
https://github.com/anx-ckreuzberger/user_foreign_key_testapp/tree/master/polls/migrations

And here is my squashed migration:
https://github.com/anx-ckreuzberger/user_foreign_key_testapp/blob/master/polls/migrations/0001_initial_squashed_0005_auto_20160414_1351.py

When applying migrations on an empty database, I am getting the following error with Django 2.0a1:

File "/home/travis/build/anx-ckreuzberger/user_foreign_key_testapp/polls/migrations/0004_actualvote.py", line 10, in <module>
    class Migration(migrations.Migration):
  File "/home/travis/build/anx-ckreuzberger/user_foreign_key_testapp/polls/migrations/0004_actualvote.py", line 22, in Migration
    ('choice', models.ForeignKey(verbose_name=b'Which choice was chosen?', to='polls.Choice')),
TypeError: __init__() missing 1 required positional argument: 'on_delete'

I have the feeling that squashing migrations is not helping in this scenario, and therefore commit 491eb56fd4dea10248ccced90c31ed64152d09cb should be adapted accordingly.

It seems the only way to get this working is to edit ALL migration files that have been created with Django 1.8 or before. Django 1.9+ should be fine AFAIK.

Version 0, edited 7 years ago by Christian Kreuzberger (next)

comment:6 by Tim Graham, 7 years ago

Resolution: invalid
Status: newclosed

After squashing migrations, you'll need to remove the old ones.

Note: See TracTickets for help on using tickets.
Back to Top