question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

postgresql returning id where sqlite works just fine

See original GitHub issue

Hello all,

i have a scenario where my dev environment works ok however i face problemas on staging/production.

here’s the faulty model:

const LembreteCatolico = Bookshelf.Model.extend({
  tableName: "lembretecatolico"
});

And this table is created like this:

.createTable("lembretecatolico", (table) => {
  table.integer("idlembrete").notNullable().references("lembrete.idlembrete");
  table.integer("idcatolico").notNullable().references("catolico.idcatolico");
  table.primary(["idlembrete", "idcatolico"]);
});

when running under sqlite, this express route works just fine:

router.post("/schedule/add", (req, res) => {
  LembreteCatolico.forge(req.body).save().then((ret) => {
    res.send(ret);
  }).catch((err) => {
    res.status(409).send("CONFLICT");
    console.log(err);
  });
});

however when running with postgres it raises the following error:

image

Relevant part is the query created:

error: insert into “lembretecatolico” (“idcatolico”, “idlembrete”) values ($1, $2) returning “id” - coluna “id” não existe

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
absoluxcommented, Jan 13, 2017

@sombriks After some research, I found the line responsible for the save, which call the knex builder, uses the idAttribute as returning value for the insert knex method. If you give it '*', it will works, but only for insert

1reaction
absoluxcommented, Jan 13, 2017

Hello @sombriks You should override the primaryKey of the model as you did for the tableName

Read more comments on GitHub >

github_iconTop Results From Across the Web

SQLite with insert returning not working as in PostgreSQL
Testing the recently welcome added "returning" clause for inserts with this query that does work in PostgreSQL but doesn't in SQLite: ...
Read more >
PSQL `returning` statement returning Unit · Issue #2372 - GitHub
This is a first draft of what the generated code should look like for a mutator statement which has a RETURNING clause.
Read more >
How to get RETURNING ID - postgresql - Stack Overflow
Use just plain INSERT ... RETURNING . And in Go use conn.Query to get the *sql.Rows (instead of *sql.Row [singular]). And then scan...
Read more >
How to Use Flask-SQLAlchemy to Interact with Databases in a ...
It provides ways to interact with several database engines such as SQLite, MySQL, and PostgreSQL. It gives you access to the database's SQL...
Read more >
Writing SQL that works on PostgreSQL, MySQL and SQLite
So REPLACE INTO basically either updates or inserts a new record. This works on both SQLite and MySQL, but not PostgreSQL. Since version...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found