Repository navigation
Closed
Description
| Component | Version |
|---|---|
| SQLAlchemy: | 1.3.0b1 |
| Python: | 3.6.6 |
| SQLite3 (binary) | 3.24.0 2018-06-04 |
In a table SQLAlchemy created as:
CREATE TABLE base_extended_json (
uid INTEGER NOT NULL,
data JSON,
PRIMARY KEY (uid)
)
In a row `I have a series of rows that have the value in their 'data' field all set to the same:
{
"key1": "val1",
"key2": "val2"
}I receive the error when selecting, the query I run is:
_sql_session.query(BaseExtendedJson).filter( sqlalchemy.cast(BaseExtendedJson.data["key2"], String) == 'value2').all()`
The error I receive is:
(sqlite3.OperationalError) no such function: JSON_EXTRACT [SQL: 'SELECT base_extended_json.uid AS base_extended_json_uid, base_extended_json.data AS base_extended_json_data \nFROM base_extended_json \nWHERE CAST(JSON_QUOTE(JSON_EXTRACT(base_extended_json.data, ?)) AS VARCHAR) = ?'] [parameters: ('$."key2"', 'value2')] (Background on this error at: http://sqlalche.me/e/e3q8) <class 'sqlalchemy.exc.OperationalError'>
At first I thought I had to specifically install the json1 extension, but then I tried the same operations in DB Browser for SQLite.
When querying via DB Browser for SQLite The following works:
SELECT json_extract(data, '$."key2"') as k2 FROM base_extended_json WHERE k2 = 'val2'
(returns a 'k2' column with a series of 'val2' rows)
While this query gets no results, but does not error:
SELECT data FROM base_extended_json WHERE cast(json_quote(json_extract(data, '$."key2"')) as varchar) = 'val2'
In a query on a different database, using json_quote does seem to work (in DB Browser), but the same error persists when running from python & SQLAlchemy.
I am at a loss as to what is up. Am I missing something, or is this a bug?
Activity
zzzeek commentedon Jan 9, 2019
can you please share the python code, e.g. simple demo of the model with the query following it so I can run it? thanks
zzzeek commentedon Jan 9, 2019
also have you tried your local sqlite3 executable? note that your Python interpreter may be built against a different version of SQLite. can you run:
erichiller commentedon Jan 9, 2019
Thanks for the quick response, very much appreciated!
my local sqlite can execute a simple query
full table:
Here is the code as a gist
And my output from the above engine version checker:
mikeywaites commentedon Jan 9, 2019
@erichiller Thanks for the extra info. I'll be taking a look to see if I can replicate this issue. Hopefully we'll get to the bottom of it fairly quickly!
mikeywaites commentedon Jan 9, 2019
@erichiller Something else that would be useful and might save me some time would be for you to run the following query and share the output please.
erichiller commentedon Jan 9, 2019
From running the above:
It appears it is not using json1 ; the same from my commandline client:
Is python using an incredibly old sqlite ? Or is it just not compiled ? Do you know if either of these, how can I resolve it ?
mikeywaites commentedon Jan 10, 2019
@erichiller thanks for getting back to us with that info. I'm afraid I don't have any answers for you. I span up a Docker container running this version of python and didn't see this issue and was able to run the script you shared. I'm not sure what OS you're using but maybe there's something different there?
You can of course enable the extensions manually but I guess that might not be ideal.
In any case as this is not an issue with SQLAlchemy i'm going to close this ticket. I'd suggest reaching out to the python community on irc or stack overflow or something.
Sorry I couldn't be more help.
zzzeek commentedon Jan 10, 2019
@erichiller looks like you're on windows so this is likely something specific to that windows build of Python 3.6. if you can specify where you got it from (was it downloaded from python.org?) you can look to see if there are newer versions that include the extension built in.
erichiller commentedon Jan 10, 2019
Thank you very much for pointing me in the right direction. Turns out to upgrade sqlite3 on windows all you need to do is download the DLLs from sqlite3's site, then copy/paste into pythondir/DLLs/ -> and overwrite the prior sqlite3.dll.
Results:
Queries now execute with errors, but also, without results -
I am still getting a lack of results with the generated query. If I manually remove the
JSON_QUOTEpart of the query, it works.Example:
Yields nothing. Nor does it yield results when I replace the placeholders and run it in DB Browser:
However, this does work:
zzzeek commentedon Jan 10, 2019
does your original test case illustrate this? also you didnt state where you got this python.exe from, should you file an issue with bugs.python.org that their windows build has a broken sqlite in it and should be shipping all necessary dlls ?
erichiller commentedon Jan 11, 2019
No results with
JSON_QUOTEbut success without occurred with the correct DLL is what I meant. My python installation was off python.org ; normal Windows x64 installer. Agreed python should have a more fully featured windows. On your development environment does the query I posted (which usesJSON_QUOTE) work?zzzeek commentedon Jan 11, 2019
i get this, is that wrong?
zzzeek commentedon Jan 11, 2019
well the test case is wrong, it's asking for "value2" when it should be "val2". following the instrucrtions at https://docs.sqlalchemy.org/en/latest/core/type_basics.html#sqlalchemy.types.JSON, the correct comparison should be:
returns a whole list of results
zzzeek commentedon Jan 11, 2019
this works too: