不要怂,就是干,撸起袖子干!

Commit 8f68c3bc by Daniel Durante

Merge pull request #890 from robraux/update-empty-postgres-array

postgres requires empty array to be explicitly cast on update
2 parents b751ced7 a317a366
...@@ -382,7 +382,7 @@ module.exports = (function() { ...@@ -382,7 +382,7 @@ module.exports = (function() {
return Utils._.template(query)(replacements) return Utils._.template(query)(replacements)
}, },
updateQuery: function(tableName, attrValueHash, where, options) { updateQuery: function(tableName, attrValueHash, where, options, attributes) {
attrValueHash = Utils.removeNullValuesFromHash(attrValueHash, this.options.omitNull, options) attrValueHash = Utils.removeNullValuesFromHash(attrValueHash, this.options.omitNull, options)
var query = "UPDATE <%= table %> SET <%= values %> WHERE <%= where %> RETURNING *" var query = "UPDATE <%= table %> SET <%= values %> WHERE <%= where %> RETURNING *"
...@@ -390,7 +390,7 @@ module.exports = (function() { ...@@ -390,7 +390,7 @@ module.exports = (function() {
for (var key in attrValueHash) { for (var key in attrValueHash) {
var value = attrValueHash[key] var value = attrValueHash[key]
values.push(this.quoteIdentifier(key) + "=" + this.escape(value)) values.push(this.quoteIdentifier(key) + "=" + this.escape(value, (!!attributes && !!attributes[key] ? attributes[key] : undefined)))
} }
var replacements = { var replacements = {
......
...@@ -417,7 +417,7 @@ module.exports = (function() { ...@@ -417,7 +417,7 @@ module.exports = (function() {
QueryInterface.prototype.update = function(dao, tableName, values, identifier, options) { QueryInterface.prototype.update = function(dao, tableName, values, identifier, options) {
var self = this var self = this
, restrict = false , restrict = false
, sql = self.QueryGenerator.updateQuery(tableName, values, identifier, options) , sql = self.QueryGenerator.updateQuery(tableName, values, identifier, options, dao.daoFactory.rawAttributes)
// Check for a restrict field // Check for a restrict field
if (!!dao.daoFactory && !!dao.daoFactory.associations) { if (!!dao.daoFactory && !!dao.daoFactory.associations) {
......
...@@ -333,7 +333,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -333,7 +333,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}) })
describe('create', function() { describe('create', function() {
it("casts empty arrays correctly for postgresql", function(done) { it("casts empty arrays correctly for postgresql insert", function(done) {
if (dialect !== "postgres" && dialect !== "postgresql-native") { if (dialect !== "postgres" && dialect !== "postgresql-native") {
expect('').to.equal('') expect('').to.equal('')
return done() return done()
...@@ -352,6 +352,30 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -352,6 +352,30 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}) })
}) })
}) })
it("casts empty array correct for postgres update", function(done) {
if (dialect !== "postgres" && dialect !== "postgresql-native") {
expect('').to.equal('')
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: [1,2,3,4], mystr: ["One", "Two", "Three", "Four"]}).on('success', function(user){
user.myvals = []
user.mystr = []
user.save().on('sql', function(sql) {
expect(sql.indexOf('ARRAY[]::INTEGER[]')).to.be.above(-1)
expect(sql.indexOf('ARRAY[]::VARCHAR[]')).to.be.above(-1)
done()
})
})
})
})
it("doesn't allow duplicated records with unique:true", function(done) { it("doesn't allow duplicated records with unique:true", function(done) {
var User = this.sequelize.define('UserWithUniqueUsername', { var User = this.sequelize.define('UserWithUniqueUsername', {
...@@ -1809,17 +1833,17 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -1809,17 +1833,17 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
it('eager loads with non-id primary keys', function(done) { it('eager loads with non-id primary keys', function(done) {
var self = this var self = this
self.User = self.sequelize.define('UserPKeagerbelong', { self.User = self.sequelize.define('UserPKeagerbelong', {
username: { username: {
type: Sequelize.STRING, type: Sequelize.STRING,
primaryKey: true primaryKey: true
} }
}) })
self.Group = self.sequelize.define('GroupPKeagerbelong', { self.Group = self.sequelize.define('GroupPKeagerbelong', {
name: { name: {
type: Sequelize.STRING, type: Sequelize.STRING,
primaryKey: true primaryKey: true
} }
}) })
self.User.belongsTo(self.Group) self.User.belongsTo(self.Group)
...@@ -1878,17 +1902,17 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -1878,17 +1902,17 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
it('eager loads with non-id primary keys', function(done) { it('eager loads with non-id primary keys', function(done) {
var self = this var self = this
self.User = self.sequelize.define('UserPKeagerone', { self.User = self.sequelize.define('UserPKeagerone', {
username: { username: {
type: Sequelize.STRING, type: Sequelize.STRING,
primaryKey: true primaryKey: true
} }
}) })
self.Group = self.sequelize.define('GroupPKeagerone', { self.Group = self.sequelize.define('GroupPKeagerone', {
name: { name: {
type: Sequelize.STRING, type: Sequelize.STRING,
primaryKey: true primaryKey: true
} }
}) })
self.Group.hasOne(self.User) self.Group.hasOne(self.User)
...@@ -2000,17 +2024,17 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -2000,17 +2024,17 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
it('eager loads with non-id primary keys', function(done) { it('eager loads with non-id primary keys', function(done) {
var self = this var self = this
self.User = self.sequelize.define('UserPKeagerone', { self.User = self.sequelize.define('UserPKeagerone', {
username: { username: {
type: Sequelize.STRING, type: Sequelize.STRING,
primaryKey: true primaryKey: true
} }
}) })
self.Group = self.sequelize.define('GroupPKeagerone', { self.Group = self.sequelize.define('GroupPKeagerone', {
name: { name: {
type: Sequelize.STRING, type: Sequelize.STRING,
primaryKey: true primaryKey: true
} }
}) })
self.Group.hasMany(self.User) self.Group.hasMany(self.User)
self.User.hasMany(self.Group) self.User.hasMany(self.Group)
...@@ -2032,7 +2056,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -2032,7 +2056,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
expect(someUser.groupPKeagerones[0].name).to.equal('people') expect(someUser.groupPKeagerones[0].name).to.equal('people')
done() done()
}) })
}) })
}) })
}) })
}) })
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!