Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion lib/postgresql.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ PostgreSQL.prototype._categorizeProperties = function(model, data) {
});
var props = Object.keys(this._models[model].properties);
var nonIdsInData = Object.keys(data).filter(function(key) {
return props.indexOf(key) !== -1 && ids.indexOf(key) === -1;
return props.indexOf(key) !== -1 && ids.indexOf(key) === -1 && data[key] !== undefined;
});
return {
ids: ids,
Expand Down Expand Up @@ -387,6 +387,9 @@ PostgreSQL.prototype.toFields = function (model, data, forCreate) {
var nonIdsInData = props.nonIdsInData;
var query = [];
if (forCreate) {
if(nonIdsInData.length == 0 && dataIdNames.length == 0) {
return 'default values';
}
query.push('(');
query.push(nonIdsInData.map(function (key) {
return self.columnEscaped(model, key);
Expand Down Expand Up @@ -1273,6 +1276,8 @@ PostgreSQL.prototype.propertySettingsSQL = function (model, propName) {
}
var result = self.columnDataType(model, propName);
if (!propertyCanBeNull.call(self, model, propName)) result = result + ' NOT NULL';

result += self.columnDbDefault(model, propName);
return result;
};

Expand Down Expand Up @@ -1384,6 +1389,20 @@ function escape(val) {
return "'" + val + "'";
}

/*!
* Get the database-default value for column from given model property
*
* @param {String} model The model name
* @param {String} property The property name
* @returns {String} The column default value
*/
PostgreSQL.prototype.columnDbDefault = function(model, property) {
var columnMetadata = this.columnMetadata(model, property);
var colDefault = columnMetadata && columnMetadata.dbDefault;

return colDefault ? (' DEFAULT ' + columnMetadata.dbDefault): '';
}

/*!
* Find the column type for a given model property
*
Expand Down
83 changes: 83 additions & 0 deletions test/postgresql.dbdefaults.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
var should = require('should'),
assert = require('assert');
var InvalidDefault, Post, db;

describe('database default field values', function () {
before(function () {
db = getDataSource();

Post = db.define('PostWithDbDefaultValue', {
created: {
type: 'Date',
postgresql: {
dbDefault: "now()"
}
},
defaultInt: {
type: 'Number',
postgresql: {
dbDefault: "5"
}
},
oneMore: {
type: 'Number'
}
});

InvalidDefault = db.define('PostWithInvalidDbDefaultValue', {
created: {
type: 'Date',
postgresql: {
dbDefault: "'5'"
}
}
});
});

it('should run migration', function (done) {
db.automigrate('PostWithDbDefaultValue', function () {
done();
});
});

it('should report inconsistent default values used', function (done) {
db.automigrate('PostWithInvalidDbDefaultValue', function (err) {
should.exists(err);
done();
});
});

it('should have \'now()\' default value in SQL column definition', function (done) {
var query = "select column_name, data_type, character_maximum_length, column_default " +
"from information_schema.columns where table_name = 'postwithdbdefaultvalue' and column_name='created'";

db.connector.query(query, function (err, results) {
assert.equal(results[0].column_default, "now()");
done(err);
});
});

it('should create a record with default value', function (done) {
Post.create({oneMore: 3}, function (err, p) {
should.not.exists(err);
Post.findOne({where: {defaultInt: 5}}, function (err, p) {
should.not.exists(err);
should.exists(p);
p.should.have.property('defaultInt', 5);
done();
});
});
});

it('should create a record with custom value', function (done) {
Post.create({oneMore: 2, defaultInt: 6}, function (err, p) {
should.not.exists(err);
Post.findOne({where: {defaultInt: 6}}, function (err, p) {
should.not.exists(err);
should.exists(p);
p.should.have.property('defaultInt', 6);
done();
});
});
});
})