'Knex simple UPDATE statement gives a syntax error
Working on a new project and encountered an issue with a simple UPDATE statement. Note that I am a Newbie to MySQL and Knex, and this issue should have a straightforward answer but apparently it doesn’t. The error:
DBError: update `password_resets` set `used` = true, `updated_at` = ‘2022-03-18 00:03:12.666’ where `id` in 2 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘2’ at line 1
at wrapError (/home/ubuntu/api/node_modules/db-errors/lib/dbErrors.js:19:14)
at handleExecuteError (/home/ubuntu/api/node_modules/objection/lib/queryBuilder/QueryBuilder.js:1489:32)
at QueryBuilder.execute (/home/ubuntu/api/node_modules/objection/lib/queryBuilder/QueryBuilder.js:670:20)
at runMicrotasks (<anonymous>)
at processTicksAndRejections (internal/process/task_queues.js:95:5) {
nativeError: Error: update `password_resets` set `used` = true, `updated_at` = ‘2022-03-18 00:03:12.666’ where `id` in 2 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘2’ at line 1
at Packet.asError (/home/ubuntu/api/node_modules/mysql2/lib/packets/packet.js:722:17)
at Query.execute (/home/ubuntu/api/node_modules/mysql2/lib/commands/command.js:28:26)
at Connection.handlePacket (/home/ubuntu/api/node_modules/mysql2/lib/connection.js:456:32)
at PacketParser.onPacket (/home/ubuntu/api/node_modules/mysql2/lib/connection.js:85:12)
at PacketParser.executeStart (/home/ubuntu/api/node_modules/mysql2/lib/packet_parser.js:75:16)
at Socket.<anonymous> (/home/ubuntu/api/node_modules/mysql2/lib/connection.js:92:25)
at Socket.emit (events.js:400:28)
at Socket.emit (domain.js:475:12)
at addChunk (internal/streams/readable.js:293:12)
at readableAddChunk (internal/streams/readable.js:267:9) {
code: ‘ER_PARSE_ERROR’,
errno: 1064,
sqlState: ‘42000’,
sqlMessage: “You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘2’ at line 1”,
sql: “update `password_resets` set `used` = true, `updated_at` = ‘2022-03-18 00:03:12.666’ where `id` in 2"
},
client: ‘mysql’
}
The line of code that produces the error:
const updatedReset = await PasswordReset.query(trx)
.update({ used: true })
.where(‘id’, reset.id)
PasswordReset.js
export class PasswordReset extends TimeStampModel {
static get tableName() {
return ‘password_resets’
}
id!: number
userId!: number
user?: User
used!: boolean
resetToken!: string
expiresAt!: Date
static relationMappings = () => ({
user: {
relation: Model.BelongsToOneRelation,
modelClass: User,
join: {
from: ‘password_resets.user_id’,
to: ‘users.id’,
},
},
})
}
It’s obvious that the SQL statement should have been
update `password_resets` set `used` = true, `updated_at` = ‘2022-03-18 00:03:12.666’ where `id` in (2);
I don’t have whereIn in the code used so I don’t even get it how it got in to begin with.
or
update `password_resets` set `used` = true, `updated_at` = ‘2022-03-18 00:03:12.666’ where `id` = 2;
Any input will be greatly appreciated. Thank you!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
