Ashutosh
Ashutosh

Reputation: 4675

Cannot insert question mark in postgres insert

I'm trying to run a simple Postgres SQL insert:

insert into "Resources" values(1, 'How are you?');

But the result after insert is:

ID        Data
---       ------
1         How are you$1

I know, to insert characters like single quote, I have to escape it with another single quote like: I can''t do it.

But what to do with ?

Upvotes: 8

Views: 4223

Answers (1)

Daniel Rearden
Daniel Rearden

Reputation: 84687

Knex interprets ? and ?? as positional bindings. You would normally use them inside a knex.raw() statement to safely inject some kind variable. For example:

knex.raw('UPDATE my_table SET my_column = ?', [someVariable])

Binding parameters like this is often necessary with raw statements to ensure whatever you're injecting is safely escaped.

So that's why you are seeing that behavior. The good news is, you can just escape question marks. From the Knex.js documentation:

To prevent replacement of ? one can use the escape sequence \\?.

Upvotes: 11

Related Questions