-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Description
What are you doing?
Sample code:
const rawSql = INSERT INTO ${tableName} (${fieldsNames}) SELECT ${escapedValues.join(", ")} WHERE NOT EXISTS( SELECT * FROM ${tableName} WHERE id = '${id}' ) LIMIT 1;
await sequelize.query(rawSql, { type: sequelize.QueryTypes.UPSERT })
ps: Since we are using fts4 table which has no primary keys, so we have to use "WHERE NOT EXISTS" to avoid duplicated data insertion. Therefore, we form a raw sql query to achieve that.
The actual executed sql would be sth like:
INSERT INTO My_FTS4_TABLE (id, content)
SELECT 'uuid-1234', 'sqlite_master'
WHERE NOT EXISTS(
SELECT * FROM My_FTS4_TABLE WHERE id = 'uuid-1234'
) LIMIT 1;
If the content contains "sqlite_master", an error will be throw after the execution (but actually the data is already inserted into the database).
What do you expect to happen?
Expected to work
What is actually happening?
Throws an error: TypeError: Cannot read property 'map' of undefined at Database.afterExecute
afterExecute /sequelize/lib/dialects/sqlite/query.js line #126:
if (self.sql.indexOf('sqlite_master') !== -1) {
result = results.map(function(resultSet) { return resultSet.name; });
} else if {
......
} else if (self.options.type === QueryTypes.UPSERT) {
result = undefined;
}
It goes into branch "if (self.sql.indexOf('sqlite_master') !== -1)", the result is undefined.
I currently fixed the problem by changing:
result = results.map(function(resultSet) { return resultSet.name; });
into:
result = results && results.map(function(resultSet) { return resultSet.name; });
BUT, I am wondering did i make any mistakes on querying raw sql like that. OR, can i simply avoid such error in a way WITHOUT modifying source code.
Dialect: sqlite
Sequelize version: 3.30.4
Tested with latest release: No
Note : Your issue may be ignored OR closed by maintainers if it's not tested against latest version OR does not follow issue template.