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

Commit 022cf04a by Mick Hansen

Fix postgres bug with blank creates

1 parent e7521c4d
...@@ -88,6 +88,7 @@ module.exports = (function() { ...@@ -88,6 +88,7 @@ module.exports = (function() {
.on('error', function(err) { .on('error', function(err) {
errorDetected = true errorDetected = true
self.emit('sql', self.sql) self.emit('sql', self.sql)
err.sql = sql
self.emit('error', err, self.callee) self.emit('error', err, self.callee)
}) })
.on('end', function(info) { .on('end', function(info) {
......
...@@ -27,6 +27,7 @@ module.exports = (function() { ...@@ -27,6 +27,7 @@ module.exports = (function() {
this.emit('sql', this.sql) this.emit('sql', this.sql)
if (err) { if (err) {
err.sql = sql
this.emit('error', err, this.callee) this.emit('error', err, this.callee)
} else { } else {
this.emit('success', this.formatResults(results)) this.emit('success', this.formatResults(results))
......
...@@ -256,9 +256,12 @@ module.exports = (function() { ...@@ -256,9 +256,12 @@ module.exports = (function() {
}, },
insertQuery: function(tableName, attrValueHash, attributes) { insertQuery: function(tableName, attrValueHash, attributes) {
var query
, valueQuery = "INSERT INTO <%= table %> (<%= attributes %>) VALUES (<%= values %>) RETURNING *;"
, emptyQuery = "INSERT INTO <%= table %> DEFAULT VALUES RETURNING *;"
attrValueHash = Utils.removeNullValuesFromHash(attrValueHash, this.options.omitNull) attrValueHash = Utils.removeNullValuesFromHash(attrValueHash, this.options.omitNull)
var query = "INSERT INTO <%= table %> (<%= attributes %>) VALUES (<%= values %>) RETURNING *;"
// Remove serials that are null or undefined, which causes an error in PG // Remove serials that are null or undefined, which causes an error in PG
Utils._.forEach(attrValueHash, function(value, key, hash) { Utils._.forEach(attrValueHash, function(value, key, hash) {
if (tables[tableName]) { if (tables[tableName]) {
...@@ -284,6 +287,8 @@ module.exports = (function() { ...@@ -284,6 +287,8 @@ module.exports = (function() {
, values: rowValues.join(",") , values: rowValues.join(",")
} }
query = replacements.attributes.length ? valueQuery : emptyQuery
return Utils._.template(query)(replacements) return Utils._.template(query)(replacements)
}, },
......
...@@ -36,6 +36,7 @@ module.exports = (function() { ...@@ -36,6 +36,7 @@ module.exports = (function() {
query.on('error', function(err) { query.on('error', function(err) {
receivedError = true receivedError = true
err.sql = sql
self.emit('error', err, self.callee) self.emit('error', err, self.callee)
}) })
......
...@@ -44,6 +44,7 @@ module.exports = (function() { ...@@ -44,6 +44,7 @@ module.exports = (function() {
self.emit('sql', self.sql) self.emit('sql', self.sql)
if (err) { if (err) {
err.sql = self.sql
onFailure.call(self, err) onFailure.call(self, err)
} else { } else {
this.columnTypes = columnTypes this.columnTypes = columnTypes
......
...@@ -898,7 +898,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() { ...@@ -898,7 +898,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
this.sequelize.sync().done(function(err) { this.sequelize.sync().done(function(err) {
expect(err).not.to.be.ok expect(err).not.to.be.ok
Worker.create().done(function (err, worker) { Worker.create({}).done(function (err, worker) {
expect(err).not.to.be.ok expect(err).not.to.be.ok
Task.bulkCreate([{}, {}]).done(function (err) { Task.bulkCreate([{}, {}]).done(function (err) {
expect(err).not.to.be.ok expect(err).not.to.be.ok
......
...@@ -595,6 +595,17 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -595,6 +595,17 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}) })
}) })
it('should allow blank creates (with timestamps: false)', function (done) {
var Worker = this.sequelize.define('Worker', {}, {timestamps: false})
Worker.sync().done(function(err) {
Worker.create().done(function (err, worker) {
expect(err).not.to.be.ok
expect(worker).to.be.ok
done()
})
})
})
describe('enums', function() { describe('enums', function() {
it('correctly restores enum values', function(done) { it('correctly restores enum values', function(done) {
var self = this var self = this
......
...@@ -415,6 +415,10 @@ if (dialect.match(/^postgres/)) { ...@@ -415,6 +415,10 @@ if (dialect.match(/^postgres/)) {
insertQuery: [ insertQuery: [
{ {
arguments: ['myTable', {}],
expectation: "INSERT INTO \"myTable\" DEFAULT VALUES RETURNING *;"
},
{
arguments: ['myTable', {name: 'foo'}], arguments: ['myTable', {name: 'foo'}],
expectation: "INSERT INTO \"myTable\" (\"name\") VALUES ('foo') RETURNING *;" expectation: "INSERT INTO \"myTable\" (\"name\") VALUES ('foo') RETURNING *;"
}, { }, {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!