Skip to content
Permalink
Browse files

Fix #280, JSON.stringify json columns

  • Loading branch information...
tgriesser committed Jun 3, 2014
1 parent e989d0a commit bd3147964cf6fb6693fe73e991494448e54721b4
Showing with 16 additions and 5 deletions.
  1. +6 −4 lib/schema/columncompiler.js
  2. +3 −1 test/integration/builder/joins.js
  3. +7 −0 test/unit/schema/postgresql.js
@@ -9,10 +9,10 @@ function ColumnCompiler(tableCompiler, columnBuilder) {
this.tableCompiler = tableCompiler;
this.columnBuilder = columnBuilder;
this.args = columnBuilder._args;
this.type = columnBuilder._type;
this.type = columnBuilder._type.toLowerCase();
this.grouped = _.groupBy(columnBuilder._statements, 'grouping');
this.modified = columnBuilder._modifiers;
this.isIncrements = (this.type.toLowerCase().indexOf('increments') !== -1);
this.isIncrements = (this.type.indexOf('increments') !== -1);
this.initCompiler();
}

@@ -44,7 +44,7 @@ ColumnCompiler.prototype.getColumnName = function() {
};

ColumnCompiler.prototype.getColumnType = function() {
var type = this[this.type.toLowerCase()];
var type = this[this.type];
return _.isFunction(type) ? type.apply(this, _.rest(this.args)) : type;
};

@@ -110,11 +110,13 @@ ColumnCompiler.prototype.defaultTo = function(value) {
return '';
} else if (value instanceof Raw) {
value = value.toQuery();
} else if (this.method === 'bool') {
} else if (this.type === 'bool') {
if (value === 'false') value = 0;
value = (value ? 1 : 0);
} else if (value === true || value === false) {
value = parseInt(value, 10);
} else if (this.type === 'json' && _.isObject(value)) {
return JSON.stringify(value);

This comment has been minimized.

Copy link
@solderjs

solderjs Jun 11, 2014

Contributor

Possibly unhandled error: syntax error at or near "["

The json values must be quoted and escaped "'" + JSON.stringify(value).replace("'", "''") + "'"

Is ' always the quote / escape character in SQL or does it vary between MySQL / PostgreSQL / SQLite?

} else {
value = "'" + value + "'";
}
@@ -624,7 +624,9 @@ module.exports = function(knex) {
.testSql(function(tester) {
tester(
'mysql',
'select `accounts`.`email` as `e1`, `a2`.`email` as `e2` from `accounts` inner join `accounts` as `a2` on `a2`.`email` <> `accounts`.`email` where `a2`.`email` = ? limit ?', ['test2@example.com', 5], [{
'select `accounts`.`email` as `e1`, `a2`.`email` as `e2` from `accounts` inner join `accounts` as `a2` on `a2`.`email` <> `accounts`.`email` where `a2`.`email` = ? limit ?',
['test2@example.com', 5],
[{
e1: 'test3@example.com',
e2: 'test2@example.com'
}, {
@@ -368,6 +368,13 @@ module.exports = function(client) {
expect(tableSql[0].sql).to.equal('alter table "users" add column "foo" bytea');
});

it('allows adding default json objects when the column is json', function() {
tableSql = new SchemaBuilder().table('user', function(t) {
t.json('preferences').defaultTo({}).notNullable();
}).toSQL();
expect(tableSql[0].sql).to.equal('alter table "user" add column "preferences" json not null {}');
});

});

};

0 comments on commit bd31479

Please sign in to comment.
You can’t perform that action at this time.