Closed prutschman-iv closed 1 day ago
It's instructive to look at what Sqlite itself reports as the column names for your query:
query = Table.select(...) # Your query.
curs = db.execute(query)
print([r[0] for r in curs.description]) # Print the column names as returned by Sqlite.
We get the following:
['id', 'json_extract("t1"."json", ?)', 'json_extract("t1"."json", ?)']
So the actual column name Sqlite gives us is json_extract("t1"."json", ?)
. Peewee attempts to clean these up, oftentimes it does a decent job, but it can't cover everything. That's why you end up with the weird dict keys. You need to use .alias()
as you described.
Alternatively you can just use Table.select().tuples()
and unpack the tuple into whatever variables you want to use.
I have some sample code below that creates an in-memory sqlite database with a single table containing a JSONField. I query it to extract 2 jsonkeys, and also prints the generated sql. The sql seems correct, but when I run the query with
.dicts()
, the resulting column name is strange, and only one of my query columns is present.When I run it, I get:
I note two things:
json", ?
. I'm guessing that this is chosen for both columns, which is why I only get one back.I can work around this with
.alias
, but it was unexpected.I'm using peewee 3.17.7, python 3.11.9, on Windows.