New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Get add_foreign_keys()
to work without modifying sqlite_master
#577
Comments
I'm certain this could work. It turns out the sqlite-utils/sqlite_utils/db.py Lines 1850 to 1872 in 1dc6b5a
Improving this code to support adding foreign keys as well would be pretty simple. And then the |
This would help address these issues, among potentially many others: |
add_foreign_keys()
work without modifying sqlite_master
?add_foreign_keys()
to work without modifying sqlite_master
Looking at the whole of the sqlite-utils/sqlite_utils/db.py Lines 1106 to 1149 in 13ebcc5
At that point we have Here's the rest of that function, which will be replaced by a called to sqlite-utils/sqlite_utils/db.py Lines 1151 to 1177 in 13ebcc5
|
Actually I think def transform(
self,
*,
# ...
# This one exists already:
drop_foreign_keys: Optional[Iterable] = None,
# These two are new. This one specifies keys to add:
add_foreign_keys: Optional[ForeignKeysType] = None,
# Or this one causes them all to be replaced with the new definitions:
foreign_keys: Optional[ForeignKeysType] = None, There should be validation that forbids you from using |
Maybe this: drop_foreign_keys: Optional[Iterable] = None, Should be this: drop_foreign_keys: Optional[Iterable[str]] = None, Because it takes a list of column names that should have their foreign keys dropped. |
As a reminder: sqlite-utils/sqlite_utils/db.py Lines 159 to 165 in 1dc6b5a
|
I'm inclined to just go with the It would be nice to drop some code complexity, plus I don't yet have a way of running automated tests against Python + SQLite versions that exhibit the problem. |
An interesting side-effect of this change is that it does result in a slightly different schema - e.g. this test: sqlite-utils/tests/test_extract.py Lines 118 to 133 in 1dc6b5a
Needs updating like so: diff --git a/tests/test_extract.py b/tests/test_extract.py
index 70ad0cf..fd52534 100644
--- a/tests/test_extract.py
+++ b/tests/test_extract.py
@@ -127,8 +127,7 @@ def test_extract_rowid_table(fresh_db):
assert fresh_db["tree"].schema == (
'CREATE TABLE "tree" (\n'
" [name] TEXT,\n"
- " [common_name_latin_name_id] INTEGER,\n"
- " FOREIGN KEY([common_name_latin_name_id]) REFERENCES [common_name_latin_name]([id])\n"
+ " [common_name_latin_name_id] INTEGER REFERENCES [common_name_latin_name]([id])\n"
")"
)
assert ( Unfortunately this means it may break other test suites that depend on I don't think this should count as a breaking change release though, but it's still worth noting. |
Plus extended tests to cover the more explicit definitions.
Here's a new plugin that brings back the |
The tests currently fail, due to migration m008_reply_to_id_foreign_key failing. I suspect this is related to a recent change in squlite-utils [0]. This PR avoids that by explictly dropping the foreign key before renaming the table, then adding the foreign key back. See the related GitHub issue for more info [1]. [0] simonw/sqlite-utils#577 [1] simonw#162
The tests currently fail, due to migration m008_reply_to_id_foreign_key failing. I suspect this is related to a recent change in squlite-utils [0]. This PR avoids that by explictly dropping the foreign key before renaming the table, then adding the foreign key back. Fixes simonw#162 [0] simonw/sqlite-utils#577
For this fix: - simonw/sqlite-utils#577 Refs #60, #116, #123
sqlite-utils/sqlite_utils/db.py
Lines 1165 to 1174 in 13ebcc5
This is the only place in the code that attempts to modify
sqlite_master
directly, which fails on some Python installations.Could this use the
.transform()
trick instead?Or automatically switch to that trick if it hits an error?
The text was updated successfully, but these errors were encountered: