Skip to content

INSERT with reserved column names ("order") needs escaping/quotes #254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
arxpoetica opened this issue Dec 19, 2021 · 3 comments · Fixed by #259
Closed

INSERT with reserved column names ("order") needs escaping/quotes #254

arxpoetica opened this issue Dec 19, 2021 · 3 comments · Fixed by #259

Comments

@arxpoetica
Copy link

The following breaks because order is a reserved POSTGRES keyword.

const some_table_data = [
  { order: 1, transition: 'up' },
  { order: 2, transition: 'down' }
]
const results = sql`INSERT INTO some_table ${sql(some_table_data, 'order', 'transition')}`

This problem is similar to #21 in that the column order needs quotes to work.

Writing this SQL raw, should look something like this:

INSERT INTO some_table ("order",transition) values ($1,$2),($3,$4);

(Notice the quotes around "order".

Instead, this library generates the following:

INSERT INTO some_table (order,transition) values ($1,$2),($3,$4);

Which throws a SQL syntax error.

I can't figure out a workaround other than to use sql.unsafe, which I'm forced to use to move forward, but I don't want that permanently. Any ideas?

@porsager
Copy link
Owner

Hi @arxpoetica .

This should be fixed to work as you'd expect. I think Postgres.js should simply quote everything in a case like this to remove any ambiguity around reserved keywords and casing.

@arxpoetica
Copy link
Author

It's not working. I'll see if I can come up with a repro.

@porsager
Copy link
Owner

No need for a repro, your initial description is perfectly fine.

@porsager porsager mentioned this issue Jan 11, 2022
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants