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.

knex violates promises contract on del()

See original GitHub issue

Hey

I think I found a pretty nasty bug regarding promises changing its value when using del(). I am not sure if other operations (like update) and/or other database engines are affected, but for sure this problem on Postgre kiasaki/alpine-postgres and knex v0.12.7.

The problem is: 1 insert a singe row to db: db('documents').insert({customer_number:1, contract_number:'b',document_number:222,contract_number_prefix:'ab'}).then(console.log) 2. Delete via del() var res = db('documents').del(); 3. resolve returned promise more than once: res.then(console.log) returns 1 (OK) res.then(console.log) returns 0 (Incorrect) res.then(console.log) returns 0 (Incorrect) res.then(console.log) returns 0 (Incorrect)

Why a subsequent resolving promise returns different value - first 1 and then 0 ?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
elhigucommented, Mar 10, 2017

@jmilkiewicz I’m not claiming that del() returns promise. It returns QueryBuilder which is thenable.

If documentation says that some of the QueryBuilder methods are returning Promise the documentation is wrong (I didn’t find anything that claims QueryBuilder being Promise, but it has an API to interact with Promises).

// instance of QueryBuilder
let queryThenable = knex('foo').del(); 

// instance of Promise (bluebird promise actually)
let resultPromise = queryThenable.then(res => res); // res == 1 (one row in the table)

// through the promise result won't change and query won't be triggered again
resultPromise.then(res => console.log("res is still 1:", res));

// through the thenable query is triggered again
queryThenable.then(res => console.log("res should be now 0:", res));

Edit: fixed variable names

0reactions
jmilkiewiczcommented, Mar 11, 2017

thx for information. If i were to design it i would make a queryBuilder immutable. It would allow one to use chain methods for where, group, sort, projection and so on. The queryBuilder would have 3 “termination” methods (probably with arity 0): select, delete, update. All of them would execute the query and return a promise (as queryBuilder is immutable - they will never mutate it). So if one would like to reuse a particular query, would need ti store queryBuilder and simple call one of terminating methods to re-execute.

You are right that in most cases you will map result of your query in then, but my query was simply a “delete”, so there is nothing to map…

Read more comments on GitHub >

github_iconTop Results From Across the Web

knex violates promises contract on del() · Issue #1952 - GitHub
Hey. I think I found a pretty nasty bug regarding promises changing its value when using del(). I am not sure if other...
Read more >
Seeding table in KnexJS foreign key constraint - Stack Overflow
I am building a NodeJS API backend and using Knex to handle DB migrations et al. I have 3 tables ...
Read more >
Interfaces | Knex.js
Promises are the preferred way of dealing with queries in knex, as they allow you to return values from a fulfillment handler, which...
Read more >
THE DIVERGENCE OF CONTRACT AND PROMISE
The remainder of my discussion investigates whether the diver- gence of contract from promise can satisfy these principles. The next two Parts argue...
Read more >
Knex.js promise resolution decoded - LeanIX Engineering Blog
This article explains some details on the Knex.js QueryBuilder interface and their impacts.
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