Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PostgreSQL should now be able to insert empty arrays with typecasting. Closes #714 #718

Merged
merged 1 commit into from
Jun 20, 2013
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
19 changes: 13 additions & 6 deletions lib/dialects/postgres/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ module.exports = (function() {
return Utils._.template(query)(options)
},

insertQuery: function(tableName, attrValueHash) {
insertQuery: function(tableName, attrValueHash, attributes) {
attrValueHash = Utils.removeNullValuesFromHash(attrValueHash, this.options.omitNull)

var query = "INSERT INTO <%= table %> (<%= attributes %>) VALUES (<%= values %>) RETURNING *;"
Expand All @@ -306,14 +306,17 @@ module.exports = (function() {
}
});

var rowValues = []
Object.keys(attrValueHash).forEach(function(attr) {
rowValues[rowValues.length] = this.escape(attrValueHash[attr], (!!attributes && !!attributes[attr] ? attributes[attr] : undefined))
}.bind(this))

var replacements = {
table: this.quoteIdentifiers(tableName)
, attributes: Object.keys(attrValueHash).map(function(attr){
return this.quoteIdentifier(attr)
}.bind(this)).join(",")
, values: Utils._.values(attrValueHash).map(function(value){
return this.escape(value)
}.bind(this)).join(",")
, values: rowValues.join(",")
}

return Utils._.template(query)(replacements)
Expand Down Expand Up @@ -734,7 +737,7 @@ module.exports = (function() {
return identifiers.split('.').map(function(t) { return this.quoteIdentifier(t, force) }.bind(this)).join('.')
},

escape: function (val) {
escape: function (val, field) {
if (val === undefined || val === null) {
return 'NULL';
}
Expand All @@ -746,7 +749,11 @@ module.exports = (function() {
return val + '';
case 'object':
if (Array.isArray(val)) {
return 'ARRAY['+ val.map(function(it) { return this.escape(it) }.bind(this)).join(',') + ']';
var ret = 'ARRAY['+ val.map(function(it) { return this.escape(it) }.bind(this)).join(',') + ']'
if (!!field && !!field.type) {
ret += '::' + field.type.replace(/\(\d+\)/g, '')
}
return ret
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/query-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ module.exports = (function() {
}

QueryInterface.prototype.insert = function(dao, tableName, values) {
var sql = this.QueryGenerator.insertQuery(tableName, values)
var sql = this.QueryGenerator.insertQuery(tableName, values, dao.daoFactory.rawAttributes)
return queryAndEmit.call(this, [sql, dao], 'insert', {
success: function(obj) { obj.isNewRecord = false }
})
Expand Down
20 changes: 20 additions & 0 deletions spec/dao-factory.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,26 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
})

describe('create', function() {
it("casts empty arrays correctly for postgresql", function(done) {
if (dialect !== "postgres" && dialect !== "postgresql-native") {
expect('').toEqual('')
return done()
}

var User = this.sequelize.define('UserWithArray', {
myvals: { type: Sequelize.ARRAY(Sequelize.INTEGER) },
mystr: { type: Sequelize.ARRAY(Sequelize.STRING) }
})

User.sync({force: true}).success(function() {
User.create({myvals: [], mystr: []}).on('sql', function(sql){
expect(sql.indexOf('ARRAY[]::INTEGER[]')).toBeGreaterThan(-1)
expect(sql.indexOf('ARRAY[]::VARCHAR[]')).toBeGreaterThan(-1)
done()
})
})
})

it("doesn't allow duplicated records with unique:true", function(done) {
var User = this.sequelize.define('UserWithUniqueUsername', {
username: { type: Sequelize.STRING, unique: true }
Expand Down