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.

`first()` turns an `update` query into a `select`

See original GitHub issue
> knex('pool_enterprise.test').update({'state': 'awesome'}).first().toSQL()
{ method: 'first',
  options: {},
  timeout: false,
  cancelOnTimeout: false,
  bindings: [ 1 ],
  __knexQueryUid: '3698eb45-5165-46af-aa43-9376e048cb27',
  sql: 'select * from "pool_enterprise"."test" limit ?' }

Probably should just throw an error? I mean you can do an UPDATE RETURNING but I don’t seen LIMIT clause allowed in the postgres docs

Tested on postgres.

How it happened is I had a limit supposed to be on a subquery but I got the parens wrong. Was a bit hard to debug.

Knex version:

knex@^0.12.9:
  version "0.12.9"
  resolved "https://registry.yarnpkg.com/knex/-/knex-0.12.9.tgz#aa852138c09ed46181e890fd698270bbe7761124"

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
elhigucommented, May 14, 2017

Thanks for the report. Throwing error sounds good.

Also probably knex should throw an error in every case where calling new builder method changes some update -> plain select.

1reaction
wubzzcommented, Feb 22, 2018

With .returning not being supported by all dialects it might not be good to give .first() two different meanings between dialects.

I would opt for just throwing an error that update was changed to select.

Read more comments on GitHub >

github_iconTop Results From Across the Web

first() turns an update query into a select #2061 - GitHub
Hm, I kinda assumed update().first() would return a single updated row, instead of an array. Maybe a .single() method would be useful for...
Read more >
How to UPDATE from a SELECT statement in SQL Server
UPDATE queries can change all tables' rows, or we can limit the update statement affects for certain rows with the help of the...
Read more >
Create and run an update query - Microsoft Support
Open the database that contains the records you want to update. On the Create tab, in the Queries group, click Query Design. Click...
Read more >
Use SELECT inside an UPDATE query - Stack Overflow
I wanted to find a string in one column and put that value in another column in the same table. The select statement...
Read more >
SELECT FOR UPDATE | CockroachDB Docs
The SELECT FOR UPDATE statement is used to order transactions by controlling concurrent access to one or more rows of a table. It...
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