question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Revision model rename breaks Page revision foreign key on SQLite

See original GitHub issue

Issue Summary

#8441 renamed the PageRevision model to Revision, which included a migration with a RenameModel step.

On my local machine, running against SQLite in the default configuration, this renamed the table but didn’t update the foreign key from the Page model. Looking at the SQL for the migration starts with:

% ./manage.py sqlmigrate wagtailcore 0070
BEGIN;
--
-- Rename model PageRevision to Revision
--
ALTER TABLE "wagtailcore_pagerevision" RENAME TO "wagtailcore_revision";
...

But if I then check the live_revision_id foreign key on the Page model in SQLite, it hasn’t been updated, and still points to the now-renamed wagtailcore_pagerevision table.

% sqlite3 db.sqlite3 
SQLite version 3.32.3 2020-06-18 14:16:19
Enter ".help" for usage hints.
sqlite> PRAGMA foreign_key_list('wagtailcore_page');
...
3|0|wagtailcore_pagerevision|live_revision_id|id|NO ACTION|NO ACTION|NONE
...

It looks like I’m getting hit by the ALTER TABLE RENAME issue described in the SQLite docs here:

Beginning with version 3.26.0, FOREIGN KEY constraints are always converted when a table is renamed, unless the PRAGMA legacy_alter_table=ON setting is engaged.

My PRAGMAs are defined thusly (the default values, apparently, for this version of SQLite on MacOS):

sqlite> PRAGMA legacy_alter_table;
1
sqlite> PRAGMA foreign_keys;
0

I note this commit that just went into Django (the dev version for 4.1) that explicitly disables legacy_alter_table; I wonder if I am hitting some edge case that this would fix.

Wagtail seems to have only one other instance of RenameModel for the SearchPromotion model, but I don’t think we have any foreign keys pointing to that. So this might be the first time this combination has hit Wagtail.

Steps to Reproduce

  1. Start a new project with wagtail start myproject
  2. cd myproject
  3. ./manage.py migrate
  4. ./manage.py createsuperuser and create an admin user.
  5. ./manage.py runserver
  6. Vist http://localhost:8000/admin/pages/add/home/homepage/3/ to create a new page (you’ll be asked to log in first)
  7. Fill in the title field with something like “Test”. Click “Save Draft”. You’ll get an error: “OperationalError at /admin/pages/add/home/homepage/3/, no such table: main.wagtailcore_pagerevision”
  • I have confirmed that this issue can be reproduced as described on a fresh Wagtail project: yes

Technical details

  • Mac OS version: Big Sur 11.6.5
  • SQLite3 version: 3.32.3 2020-06-18 14:16:19 02c344aceaea0d177dd42e62c8541e3cab4a26c757ba33b3a31a43ccc7d4aapl
  • Python version: 3.10.4
  • Django version: 4.0.5, also happens on 3.2
  • Wagtail version: main (1f43d8ef51e455b92e42447fdc190d5ec83ec53c)

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:11 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
laymonagecommented, Jun 8, 2022

Thanks for the report!

I think it’s worth noting that the SQLite installed on the system is not always the same as the SQLite bundled in Python. In my case:

image

As the legacy_alter_table is 0 on my Python’s SQLite, I did not encounter the issue.

My environment:

  • macOS Monterey 12.4 on MacBook Pro M1 Pro
  • Python 3.10.2 installed from Python.org
  • SQLite version: 3.35.5 (Python), 3.37.0 (System)
  • Django version: 4.0.5
  • Wagtail version: main
0reactions
gasmancommented, Aug 12, 2022

(Hopefully) fixed in #8965.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SQLite Foreign Key Support
This document describes the support for SQL foreign key constraints introduced in SQLite version 3.6.19 (2009-10-14).
Read more >
How do I add a foreign key to an existing SQLite table?
Use CREATE TABLE to construct a new table "new_X" that is in the desired revised format of table X. Make sure that the...
Read more >
Fixing ALTER TABLE errors with Flask-Migrate and SQLite
A nasty type of issue occurs when the ALTER TABLE error occurs in the middle of a migration, after some operations were already...
Read more >
SQLite Foreign Key: Enforce Relationships Between Tables
This tutorial shows you how to use the SQLite foreign key constraint to enforce the relationships between correlated tables.
Read more >
Releases - NocoDB Documentation
[closed] Bug: No data displayed when navigating from a table on page 2 to a ... Next Release] External MS SQL Server bug...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found