Closed punkish closed 2 months ago
Thanks for taking the time to describe this issue.
I’d strongly suggest altering your query to use unique column names, though. I suspect duplicate column or alias names in your SELECT may cause other non deterministic behaviors (as with any rdbms), including references in ORDER BY and GROUP BY clauses.
Causes the prepared statement to return data namespaced by table. Each key in a row object will be a table name, and each corresponding value will be a nested object that contains the associated column data. This is useful when performing a JOIN between two tables that have overlapping column names. If a result column is an expression or subquery, it will be available within the special $ namespace.
Alternatively .raw(true)
will also give you all data. Combine it with .columns()
at least until either
better-sqlite3
or SQLite change their behavior.
What does that mean? You are asking the database for results with ambiguous names, so it's up to you to handle that properly. And better-sqlite3 offers all tools needed.
yea, you are correct. Now I know that better-sqlite3 does offer the tools I need to handle this curious situation. The only possible improvement would be if better-sqlite3 figured out that the result had ambiguous names and automagically used .expand(true)
. In any case, many thanks for the explanation.
note: this is cross-posted here from the SQLite3 forums.
Consider what the SQLite CLI does when returning results with same column names from different tables but no aliases.
So, SQLite (the lib) is happy to output columns with the same default aliases, when none are provided, as shown in
SELECT * FROM one JOIN two ON one.id = two.one_id
but at least the SQLite CLI doesn't like it when ambiguous column names are provided in a query.better-sqlite3
, on the other hand, silently removes the columntwo.a
from the result because the returning object is a JavaScript object, and an object can't have two keys with the same name.Now that I know this behavior, I can code around it, at least until either
better-sqlite3
or SQLite change their behavior. Frankly, I don't know what the right solution should be. I am assuming SQLite behaves the way it does because, perhaps all SQL engines do the same thing.better-sqlite3
, on the other hand, does the best with what it is provided -- a resulting data set that is modeled as an array (table) of objects (column-value pairs). But the objects can't have duplicate keys. Hence, we have a problem unless SQLite sends some other info in the result.Of course, I am reporting this to the
better-sqlite3
folks as well.