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

Commit 0bfc4e31 by Mick Hansen

fix uuid default values for primary keys named id

1 parent 77f3c2b4
...@@ -649,10 +649,6 @@ module.exports = (function() { ...@@ -649,10 +649,6 @@ module.exports = (function() {
if (options.isNewRecord) { if (options.isNewRecord) {
defaults = {} defaults = {}
// set id to null if not passed as value, a newly created dao has no id
// removing this breaks bulkCreate
defaults[this.Model.primaryKeyAttribute] = null;
if (this.Model._hasDefaultValues) { if (this.Model._hasDefaultValues) {
Utils._.each(this.Model._defaultValues, function(valueFn, key) { Utils._.each(this.Model._defaultValues, function(valueFn, key) {
if (!defaults.hasOwnProperty(key)) { if (!defaults.hasOwnProperty(key)) {
...@@ -661,6 +657,13 @@ module.exports = (function() { ...@@ -661,6 +657,13 @@ module.exports = (function() {
}) })
} }
// set id to null if not passed as value, a newly created dao has no id
// removing this breaks bulkCreate
// do after default values since it might have UUID as a default value
if (!defaults.hasOwnProperty(this.Model.primaryKeyAttribute)) {
defaults[this.Model.primaryKeyAttribute] = null;
}
if (this.Model._timestampAttributes.createdAt && defaults[this.Model._timestampAttributes.createdAt]) { if (this.Model._timestampAttributes.createdAt && defaults[this.Model._timestampAttributes.createdAt]) {
this.dataValues[this.Model._timestampAttributes.createdAt] = Utils.toDefaultValue(defaults[this.Model._timestampAttributes.createdAt]); this.dataValues[this.Model._timestampAttributes.createdAt] = Utils.toDefaultValue(defaults[this.Model._timestampAttributes.createdAt]);
delete defaults[this.Model._timestampAttributes.createdAt]; delete defaults[this.Model._timestampAttributes.createdAt];
......
...@@ -130,6 +130,30 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -130,6 +130,30 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}) })
describe('create', function() { describe('create', function() {
it('works with non-integer primary keys', function (done) {
var User = this.sequelize.define('User', {
'id': {
primaryKey: true,
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4
},
'email': {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4
}
})
this.sequelize.sync({force: true}).done(function (err) {
expect(err).not.to.be.ok
User.create({}).done(function (err, user) {
expect(err).not.to.be.ok
expect(user).to.be.ok
expect(user.id).to.be.ok
done()
})
})
})
it('supports transactions', function(done) { it('supports transactions', function(done) {
var self = this var self = this
......
...@@ -661,6 +661,20 @@ describe(Support.getTestDialectTeaser("DAO"), function () { ...@@ -661,6 +661,20 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
expect(uuid.parse(user.uuidv4)).to.have.length(16) expect(uuid.parse(user.uuidv4)).to.have.length(16)
done() done()
}) })
it('should store a valid uuid if the field is a primary key named id', function () {
var Person = this.sequelize.define('Person', {
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV1,
primaryKey: true
}
})
var person = Person.build({})
expect(person.id).to.be.ok
expect(person.id).to.have.length(36)
})
}) })
describe('current date', function() { describe('current date', function() {
it('should store a date in touchedAt', function(done) { it('should store a date in touchedAt', function(done) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!