Skip to content

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

Closed
@arxpoetica

Description

@arxpoetica

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions