升级Wagtail 2.0时遭遇数据库迁移错误,请求技术支持
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
Edit the problematic migration file
Open/Users/tim/wagtailcmsdemo/home/migrations/0001_initial.pyand find the line with theOneToOneFieldforpage_ptr. Addon_delete=models.CASCADEto 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 modelsat the top (it almost certainly will, since it’s generated automatically).Check other migration files
Scan through migrations in other apps (likewagtailcoreor custom apps) to make sure no otherForeignKeyorOneToOneFieldentries are missing theon_deleteparameter. Fix any you find the same way.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




