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.

Extract columns cannot create foreign key relation: sqlite3.OperationalError: table sqlite_master may not be modified

See original GitHub issue

Thanks for what seems like a truly great suite of libraries. I wanted to try out Datasette, but never got more than half way through your YouTube video with the SF tree dataset. Whenever I try to extract a column, I get a sqlite3.OperationalError: table sqlite_master may not be modified error from Python. This snippet reproduces the error on my system, Python 3.9.1 and sqlite-utils 3.5 on an M1 Macbook Pro running in rosetta mode:

curl "https://data.nasa.gov/resource/y77d-th95.json" | \
    sqlite-utils insert meteorites.db meteorites - --pk=id
sqlite-utils extract meteorites.db meteorites  recclass

I have tried googling the problem, but all I’ve found is that this might be a problem with the sqlite3 database running in defensive mode, but I definitely can’t know for sure. Does the problem seem familiar to you?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:8

github_iconTop GitHub Comments

1reaction
wpearscommented, Aug 23, 2022

This bug affects me as well. Env:

Python 3.8.12
sqlite-utils, version 3.28
sqlite3 3.32.3
MacOS Big Sur 11.6.7
Intel

Similar to @mdrovdahl, I was able to work around this bug by piping the SQL string constructed in add_foreign_keys to the sqlite3 command itself. Specifically, if you’re trying to patch this yourself, replace lines 1026-1039 of db.py in your site packages with something similar to the following:

print("PRAGMA writable_schema = 1;")
for table_name, new_sql in table_sql.items():
    print("UPDATE sqlite_master SET sql = '{}' WHERE name = '{}';".format(
         new_sql, table_name)
    )
print("PRAGMA writable_schema = 0;")
print("VACUUM;")

Then from your terminal: db-to-sqlite "<connection string>" your.db --all > output.sql && sqlite3 your.db < output.sql

If you want to run this with -p, you’ll have to actually open a file in code to write to instead of redirecting the output.

0reactions
hydrosquallcommented, Dec 16, 2022

A related historical problem:

https://github.com/tekartik/sqflite/issues/525#issuecomment-714500720

I wonder if the version of Sqlite or Python for Intel chip have defensive mode disabled by default, whereas M1 chips versions have it enabled.

Read more comments on GitHub >

github_iconTop Results From Across the Web

android - SQLite Foreign Key - Stack Overflow
You tried to create FK on non-existing column and this is reason why it doesn't work. So you need to add this: checklist_id...
Read more >
sqlite_utils Python library — sqlite-utils 3.15 documentation
Custom column order and column types; Explicitly creating a table; Compound primary keys; Specifying foreign keys. Table configuration options; Setting ...
Read more >
sqlite3 — DB-API 2.0 interface for SQLite databases — Python ...
Call sqlite3.connect() to to create a connection to the database tutorial.db in the current working directory, implicitly creating it if it does not...
Read more >
SQLite Foreign Key Support
In order to use foreign key constraints in SQLite, the library must be compiled with neither SQLITE_OMIT_FOREIGN_KEY nor SQLITE_OMIT_TRIGGER ...
Read more >
#29182 (SQLite 3.26 breaks database migration ForeignKey ...
This happens only when running SQLite DB migrations from management command under transaction. Running migrations via ./manage.py migrate does not cause any ...
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