Knex: Question regarding use of then() with Knex.js and Postgres

Created on 16 Jan 2014  ·  3Comments  ·  Source: knex/knex

I am trying to use then() to chain my create table and insertion of rows.
The table is created but no insertion happens (no error is caught).
Can someone tell me where I am wrong?

Thanks
MN

var Promise = require('bluebird');
var Knex = require('knex');
Knex.knex = Knex.initialize({
client: 'pg',
connection: {

},
debug: true
});
var knex = require('knex').knex;
knex.schema.createTable('department', function(table) {
table.increments('department_id').primary().notNullable();
table.string('department_name', 80).unique().notNullable();
table.string('building_name', 40);
}).then (function() {
console.log('department table has been created');
knex('department').insert(
[{department_name:'Nano Technology', building_name:'block-A'},
{department_name:'Mechanical Engineering', building_name:'block-C'}],
'department_id'
);
}).then(function() {
console.log('departments inserted');
}).catch(function(e) {
console.log(e);
});

question

Most helpful comment

You have to either return your knex insert statement, or add handlers to it (by using then or catch/ caught), else you are just creating a builder instance but are not actually executing the query. When you call methods like insert, select, frometc. - basically every method on the query builder - it will only modify its internal state, but not actually execute the query. That will only happen when you add one or more handlers to the instance using then, catch/caught, lastly/finally,tap or exec, because this will then create the actual Promise, which sends the query to the database client.

All 3 comments

You have to either return your knex insert statement, or add handlers to it (by using then or catch/ caught), else you are just creating a builder instance but are not actually executing the query. When you call methods like insert, select, frometc. - basically every method on the query builder - it will only modify its internal state, but not actually execute the query. That will only happen when you add one or more handlers to the instance using then, catch/caught, lastly/finally,tap or exec, because this will then create the actual Promise, which sends the query to the database client.

Knex and Bookshelf are great. Thanks a lot

  • MN

On 16 Jan 2014, at 13:17, Johannes Lumpe [email protected] wrote:

You have to either return your knex insert statement, or add handlers to it (by using then or catch/ caught), else you are just creating a builder instance but are not actually executing the query. When you call methods like insert, select, frometc. - basically every method on the query builder - it will only modify its internal state, but not actually execute the query. That will only happen when you add one or more handlers to the instance using then, catch/caught, lastly/finally,tap or exec, because this will then create the actual Promise, which sends the query to the database client.


Reply to this email directly or view it on GitHub.

You're welcome!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

olaferlandsen picture olaferlandsen  ·  3Comments

rarkins picture rarkins  ·  3Comments

nklhrstv picture nklhrstv  ·  3Comments

koskimas picture koskimas  ·  3Comments

lanceschi picture lanceschi  ·  3Comments