diff --git a/lib/migration.js b/lib/migration.js index fe0058c2..c1dee3c3 100644 --- a/lib/migration.js +++ b/lib/migration.js @@ -236,7 +236,8 @@ function mixinMigration(PostgreSQL) { var prop = this.getModelDefinition(model).properties[propName]; var sqlCommand = self.columnEscaped(model, propName) + ' ' + self.columnDataType(model, propName) + - (self.isNullable(prop) ? '' : ' NOT NULL'); + (self.isNullable(prop) ? '' : ' NOT NULL') + + self.columnDbDefault(model, propName); return sqlCommand; }; @@ -496,7 +497,8 @@ function mixinMigration(PostgreSQL) { }; /*! - * Get the database-default value for column from given model property + * Get the database-default value for column from given model property. + * Falls back to LDL's prop.default. * * @param {String} model The model name * @param {String} property The property name @@ -505,8 +507,14 @@ function mixinMigration(PostgreSQL) { PostgreSQL.prototype.columnDbDefault = function(model, property) { var columnMetadata = this.columnMetadata(model, property); var colDefault = columnMetadata && columnMetadata.dbDefault; + if (!colDefault) { + var prop = this.getModelDefinition(model).properties[property]; + if (prop.hasOwnProperty('default')) { + colDefault = String(this.escapeValue(prop.default)); + } + } - return colDefault ? (' DEFAULT ' + columnMetadata.dbDefault) : ''; + return colDefault ? (' DEFAULT ' + colDefault) : ''; }; /*! diff --git a/lib/postgresql.js b/lib/postgresql.js index 865d005b..329a4ef6 100644 --- a/lib/postgresql.js +++ b/lib/postgresql.js @@ -345,6 +345,10 @@ PostgreSQL.prototype.escapeValue = function(value) { if (typeof value === 'number' || typeof value === 'boolean') { return value; } + // Can't send functions, objects, arrays + if (typeof value === 'object' || typeof value === 'function') { + return null; + } return value; }; diff --git a/test/postgresql.migration.test.js b/test/postgresql.migration.test.js index 07077385..effcf357 100644 --- a/test/postgresql.migration.test.js +++ b/test/postgresql.migration.test.js @@ -14,9 +14,7 @@ describe('migrations', function() { before(setup); it('should run migration', function(done) { - db.automigrate('UserDataWithIndexes', function() { - done(); - }); + db.automigrate('UserDataWithIndexes', done); }); it('UserDataWithIndexes should have correct indexes', function(done) { @@ -89,6 +87,7 @@ function setup(done) { birthDate: Date, pendingPeriod: Number, createdByAdmin: Boolean, + deleted: {type: Boolean, required: true, default: false}, }, { indexes: { udwi_index1: {