You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

升级Wagtail 2.0时遭遇数据库迁移错误,请求技术支持

Fixing "TypeError: init() missing 1 required positional argument: 'on_delete'" in Wagtail 2.0 Migration

Hey Tim, I’ve run into this exact issue when upgrading Wagtail to 2.0 before—let’s get it sorted out quickly!

Why This Happens

Wagtail 2.0 requires Django 2.0 or later, and starting with Django 2.0, every ForeignKey and OneToOneField must include the on_delete parameter. This parameter tells Django what to do with your model instance when the linked object (in this case, a Wagtail Page) gets deleted. Your existing migration file was created with an older version of Django/Wagtail that didn’t enforce this rule, hence the error.

Step-by-Step Fix

  1. Edit the problematic migration file
    Open /Users/tim/wagtailcmsdemo/home/migrations/0001_initial.py and find the line with the OneToOneField for page_ptr. Add on_delete=models.CASCADE to the arguments—this is the standard behavior for Wagtail pages (if a parent page is deleted, its child pages should be deleted too).

    The corrected line should look like this:

    ('page_ptr', models.OneToOneField(parent_link=True, auto_created=True, primary_key=True, serialize=False, to='wagtailcore.Page', on_delete=models.CASCADE)),
    

    Double-check that the migration file already has from django.db import models at the top (it almost certainly will, since it’s generated automatically).

  2. Check other migration files
    Scan through migrations in other apps (like wagtailcore or custom apps) to make sure no other ForeignKey or OneToOneField entries are missing the on_delete parameter. Fix any you find the same way.

  3. Re-run migrations
    Execute these commands in your terminal to apply the fixes:

    ./manage.py makemigrations
    ./manage.py migrate
    

Quick Note for Future Upgrades

When upgrading Wagtail versions, always refer to the official upgrade guide first—they explicitly call out Django version requirements and breaking changes like this one, which can save you a lot of troubleshooting time!

内容的提问来源于stack exchange,提问作者TimK

火山引擎 最新活动