Skip to content
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

Cannot escape column names #97

Closed
jussikinnula opened this issue Jul 31, 2020 · 3 comments
Closed

Cannot escape column names #97

jussikinnula opened this issue Jul 31, 2020 · 3 comments

Comments

@jussikinnula
Copy link

jussikinnula commented Jul 31, 2020

Test case:

// postgres_restricted_word_insert.ts
import postgres from 'postgres';

(async function run() {
  const sql = postgres(process.env.DATABASE_URL || 'postgres://localhost:5432/test');

  await sql`
    DROP TABLE IF EXISTS users
  `;

  await sql`
    CREATE TABLE users (
      "user" varchar(255) NOT NULL,
      CONSTRAINT users_pk PRIMARY KEY ("user")
    );
  `;

  // Works
  console.info('Step 1: Insert user with manually escaping restricted word');
  const rows = await sql`
    INSERT INTO users ("user") VALUES('test user')
  `;
  console.info('Step 1: Completed\n')

  // Does not work
  console.info('Step 2: Insert user by using JS object');
  const user = { user: 'another test user' };
  await sql`
    INSERT INTO users ${sql(user, 'user')}
  `.catch(() => {
    console.error('Step 2: As expected, it did fail...');
    return Promise.resolve();
  });
  console.info('Step 2: Completed\n');

  console.info('All done!');

  process.exit();
})();
@akheron
Copy link
Contributor

akheron commented Aug 1, 2020

There's a typo in the second insert statement. The table name should be users instead of foo.

@jussikinnula
Copy link
Author

There's a typo in the second insert statement. The table name should be users instead of foo.

Sorry about the typo. It is now fixed. Anyway, if changing the table column name user to for example user2 - both of the cases work.

There's a list of reserved words, which need quotes. So there should be anyway some way to use column names with those reserved words, like on the Step 1 one can do.

I'm talking about this list: https://www.postgresql.org/docs/8.1/sql-keywords-appendix.html

@akheron
Copy link
Contributor

akheron commented Aug 1, 2020

Yeah. It would probably be easiest to just quote all column names.

porsager added a commit that referenced this issue Mar 23, 2021
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

No branches or pull requests

3 participants