Search code examples
knex.js

Knex insert gives error: syntax error at or near "("


The following insert gives an error:

return knex('accounts')
    .returning('id')
    .insert(account)
    .then(ids => {
        account.id = ids[0];
        return account;
    });

syntax error at or near "("

The debug log is shown below:

{ 
  method: 'insert',
  options: {},
  timeout: false,
  cancelOnTimeout: false,
  bindings: [ 'Fidelity Mastercard' ],
  __knexQueryUid: '7c0e3f8e-9de4-4a4d-8465-7f00e35a4701',
  sql: 'insert into  ("name") values (?) returning "id"',
  returning: 'id'
}

What am I doing wrong?

Database is PostgrSQL and is initialized as shown below:

const dbConfig = {
    client: 'pg',
    debug: true,
    connection: {
        host: 'localhost',
        user: 'postgres',
        password: 'docker',
        database: 'mmm',
        charset: 'utf8'
    }
};

Edit

Please ignore this question. I made a silly mistake in coding. In the example above, knex was really a function returning knex. So the call should have been knex()('accounts'). Mystery solved!


Solution

  • As seen in my edit above, this was a coding mistake on my side.