Skip to content

Instantly share code, notes, and snippets.

@chrisfrancis27
Created March 11, 2016 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisfrancis27/135424977eca74119fd4 to your computer and use it in GitHub Desktop.
Save chrisfrancis27/135424977eca74119fd4 to your computer and use it in GitHub Desktop.
Knex 0.10.0 `undefined -> DEFAULT` test
'use strict';
exports.up = function(knex) {
return knex.schema
.createTable('things', t => {
t.increments('id').primary().unsigned();
t.text('a').notNullable().defaultTo('');
t.integer('b').notNullable().defaultTo(0);
t.boolean('c').notNullable().defaultTo(false);
})
;
};
exports.down = function(knex) {
return knex.schema.dropTableIfExists('things');
};
'use strict';
exports.seed = knex => {
let seeds = [{
a: 'lorem',
b: 123
}, {
a: 'ipsum',
b: 456
}, {
a: 'dolor',
b: 789,
c: true
}];
let q = knex('things').insert(seeds);
console.log(`QUERY: ${q.toString()}`);
return q;
};
@chrisfrancis27
Copy link
Author

Output:

QUERY: insert into "things" ("a", "b", "c") values ('lorem', '123', DEFAULT), ('ipsum', '456', DEFAULT), ('dolor', '789', 'true')
{ method: 'insert',
  options: {},
  bindings:
   [ 'lorem',
     123,
     undefined,
     'ipsum',
     456,
     undefined,
     'dolor',
     789,
     true ],
  sql: 'insert into "things" ("a", "b", "c") values (?, ?, ?), (?, ?, ?), (?, ?, ?)',
  returning: undefined }
error: insert into "things" ("a", "b", "c") values ($1, $2, $3), ($4, $5, $6), ($7, $8, $9) - null value in column "c" violates not-null constraint
    at Connection.parseE (/tmp/node_modules/pg/lib/connection.js:539:11)
    at Connection.parseMessage (/tmp/node_modules/pg/lib/connection.js:366:17)
    at Socket.<anonymous> (/tmp/node_modules/pg/lib/connection.js:105:22)
    at emitOne (events.js:78:13)
    at Socket.emit (events.js:170:7)
    at readableAddChunk (_stream_readable.js:147:16)
    at Socket.Readable.push (_stream_readable.js:111:10)
    at TCP.onread (net.js:524:20)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment