[Django] #28677: on_delete is a required positional argument for ForeignKeys, even in migrations

275 views
Skip to first unread message

Django

unread,
Oct 3, 2017, 8:08:44 AM10/3/17
to django-...@googlegroups.com
#28677: on_delete is a required positional argument for ForeignKeys, even in
migrations
-------------------------------------+-------------------------------------
Reporter: Christian | Owner: nobody
Kreuzberger |
Type: Bug | Status: new
Component: | Version: 2.0
Migrations | Keywords: migration
Severity: Normal | foreignkey on_delete
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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.

--
Ticket URL: <https://code.djangoproject.com/ticket/28677>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Oct 3, 2017, 8:09:10 AM10/3/17
to django-...@googlegroups.com
#28677: on_delete is a required positional argument for ForeignKeys, even in
migrations
-------------------------------------+-------------------------------------
Reporter: Christian | Owner: nobody
Kreuzberger |
Type: Bug | Status: new
Component: Migrations | Version: 2.0
Severity: Normal | Resolution:
Keywords: migration | Triage Stage:
foreignkey on_delete | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Christian Kreuzberger):

* Attachment "0005_auto_20160414_1351.py" added.

Django

unread,
Oct 3, 2017, 9:12:32 AM10/3/17
to django-...@googlegroups.com
#28677: on_delete is a required positional argument for ForeignKeys, even in
migrations
-------------------------------------+-------------------------------------
Reporter: Christian | Owner: nobody
Kreuzberger |
Type: Bug | Status: closed
Component: Migrations | Version: 2.0
Severity: Normal | Resolution: invalid

Keywords: migration | Triage Stage:
foreignkey on_delete | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* status: new => closed
* resolution: => invalid


Comment:

Yes, it's required in migrations
[https://docs.djangoproject.com/en/dev/releases/1.9/#foreignkey-and-
onetoonefield-on-delete-argument as documented]. Consider squashing
migrations so you don't have many to update.

--
Ticket URL: <https://code.djangoproject.com/ticket/28677#comment:1>

Django

unread,
Oct 3, 2017, 9:46:59 AM10/3/17
to django-...@googlegroups.com
#28677: on_delete is a required positional argument for ForeignKeys, even in
migrations
-------------------------------------+-------------------------------------
Reporter: Christian | Owner: nobody
Kreuzberger |
Type: Bug | Status: closed
Component: Migrations | Version: 2.0
Severity: Normal | Resolution: invalid
Keywords: migration | Triage Stage:
foreignkey on_delete | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"491eb56fd4dea10248ccced90c31ed64152d09cb" 491eb56]:
{{{
#!CommitTicketReference repository=""
revision="491eb56fd4dea10248ccced90c31ed64152d09cb"
Refs #28677 -- Doc'd that on_delete is required in migrations.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/28677#comment:2>

Django

unread,
Oct 3, 2017, 9:47:11 AM10/3/17
to django-...@googlegroups.com
#28677: on_delete is a required positional argument for ForeignKeys, even in
migrations
-------------------------------------+-------------------------------------
Reporter: Christian | Owner: nobody
Kreuzberger |
Type: Bug | Status: closed
Component: Migrations | Version: 2.0
Severity: Normal | Resolution: invalid
Keywords: migration | Triage Stage:
foreignkey on_delete | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"cfde4ac3e7f2595c060be59bddf6daabbbc9e4c3" cfde4ac3]:
{{{
#!CommitTicketReference repository=""
revision="cfde4ac3e7f2595c060be59bddf6daabbbc9e4c3"
[2.0.x] Refs #28677 -- Doc'd that on_delete is required in migrations.

Backport of 491eb56fd4dea10248ccced90c31ed64152d09cb from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/28677#comment:3>

Django

unread,
Oct 3, 2017, 9:48:48 AM10/3/17
to django-...@googlegroups.com
#28677: on_delete is a required positional argument for ForeignKeys, even in
migrations
-------------------------------------+-------------------------------------
Reporter: Christian | Owner: nobody
Kreuzberger |
Type: Bug | Status: closed
Component: Migrations | Version: 2.0
Severity: Normal | Resolution: invalid
Keywords: migration | Triage Stage:
foreignkey on_delete | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Christian Kreuzberger):

Replying to [comment:3 Tim Graham <timograham@…>]:


> In [changeset:"cfde4ac3e7f2595c060be59bddf6daabbbc9e4c3" cfde4ac3]:
> {{{
> #!CommitTicketReference repository=""
revision="cfde4ac3e7f2595c060be59bddf6daabbbc9e4c3"
> [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!

--
Ticket URL: <https://code.djangoproject.com/ticket/28677#comment:4>

Django

unread,
Oct 15, 2017, 3:16:39 PM10/15/17
to django-...@googlegroups.com
#28677: on_delete is a required positional argument for ForeignKeys, even in
migrations
-------------------------------------+-------------------------------------
Reporter: Christian | Owner: nobody
Kreuzberger |
Type: Bug | Status: new
Component: Migrations | Version: 2.0
Severity: Normal | Resolution:
Keywords: migration | Triage Stage:
foreignkey on_delete | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Christian Kreuzberger):

* status: closed => new
* resolution: invalid =>


Comment:

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/28677#comment:5>

Django

unread,
Oct 15, 2017, 5:47:55 PM10/15/17
to django-...@googlegroups.com
#28677: on_delete is a required positional argument for ForeignKeys, even in
migrations
-------------------------------------+-------------------------------------
Reporter: Christian | Owner: nobody
Kreuzberger |
Type: Bug | Status: closed
Component: Migrations | Version: 2.0
Severity: Normal | Resolution: invalid

Keywords: migration | Triage Stage:
foreignkey on_delete | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* status: new => closed
* resolution: => invalid


Comment:

After squashing migrations, you'll need to
[https://docs.djangoproject.com/en/dev/topics/migrations/#squashing-
migrations remove the old ones].

--
Ticket URL: <https://code.djangoproject.com/ticket/28677#comment:6>

Reply all
Reply to author
Forward
0 new messages