You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Can not execute raw SQL queries for sqlite3 (sequelize.query) #5755
q = db.query(
`SELECT c.* FROM cities c ....`,
{
type: db.QueryTypes.SELECT,
model: City,
replacements: { ... }
}
);
But I'm getting this error:
Executed (default): SELECT c.* FROM ... in 5ms
/Users/laise/Work/itowl/eps-online-service-countries/node_modules/sqlite3/lib/trace.js:28
throw err;
^
TypeError: Cannot read property 'geonameid' of undefined
at /Users/laise/Work/itowl/eps-online-service-countries/node_modules/sequelize/lib/dialects/sqlite/query.js:161:46
at /Users/laise/Work/itowl/eps-online-service-countries/node_modules/lodash/lodash.js:12449:23
at /Users/laise/Work/itowl/eps-online-service-countries/node_modules/lodash/lodash.js:4332:15
at baseForOwn (/Users/laise/Work/itowl/eps-online-service-countries/node_modules/lodash/lodash.js:2731:24)
SQL is valid and successfully executes.
Furthemore, a query with (undocumented?) raw: true option set executes succesfully:
q = db.query(
`SELECT c.* FROM cities c ....`,
{
raw: true,
type: db.QueryTypes.SELECT,
model: City,
replacements: { ... }
}
);
The source of the issue is type = tableTypes[name] assignment here. tableTypes is always undefined, because it is being assigned in tableTypes = columnTypes[tableName] line here and columnTypes is always equal to {}.
q = db.query(
`SELECT c.* FROM cities c ....`,
{
tableNames: ["cities"],
type: db.QueryTypes.SELECT,
model: City,
replacements: { ... }
}
);
Seems like the adapter was not able to deduce a table name from my SQL.
I was not able to find any documentation on raw and tableNames params.
I think it needs to be documented somewhere.
SELECT c.* FROM ft_search f LEFT JOIN cities c ON f.geonameid == c.geonameid
WHERE f.names MATCH :prefix AND c.country = :countryAlpha2
ORDER BY c.population DESC
LIMIT :count OFFSET :skip
And I see that you handle only FROM ... tables. May be JOIN ... tables should be handled too?
Generally you want to use raw: true, it should probably be the default actually.
When using non-raw queries sequelize assumes (poorly) that it's an internal call that has all the correct internal values.
Activity
907th commentedon Apr 15, 2016
This query works too:
Seems like the adapter was not able to deduce a table name from my SQL.
I was not able to find any documentation on
rawandtableNamesparams.I think it needs to be documented somewhere.
907th commentedon Apr 15, 2016
A full query:
And I see that you handle only
FROM ...tables. May beJOIN ...tables should be handled too?mickhansen commentedon Apr 18, 2016
Generally you want to use
raw: true, it should probably be the default actually.When using non-raw queries sequelize assumes (poorly) that it's an internal call that has all the correct internal values.
ivo-stefchev commentedon May 20, 2016
sequelize/lib/dialects/sqlite/query.js
columnTypes never sets any key, hence tableTypes would always be undefined.
add default value for tableTypes in sqlite (#5755) (#5931)
witem commentedon Nov 16, 2016
Can you fix this in v3 branch?
In version 3.25.0 line 147:
, tableTypes = columnTypes[tableName];felixfbecker commentedon Nov 16, 2016
Feel free to backport
witem commentedon Nov 16, 2016
@felixfbecker done