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

Commit 786b19b9 by Sushant

fix(build): default null for multiple primary keys

1 parent ae7d4b96
...@@ -123,8 +123,12 @@ class Model { ...@@ -123,8 +123,12 @@ class Model {
// set id to null if not passed as value, a newly created dao has no id // set id to null if not passed as value, a newly created dao has no id
// removing this breaks bulkCreate // removing this breaks bulkCreate
// do after default values since it might have UUID as a default value // do after default values since it might have UUID as a default value
if (this.constructor.primaryKeyAttribute && !defaults.hasOwnProperty(this.constructor.primaryKeyAttribute)) { if (this.constructor.primaryKeyAttributes.length) {
defaults[this.constructor.primaryKeyAttribute] = null; this.constructor.primaryKeyAttributes.forEach(primaryKeyAttribute => {
if (!defaults.hasOwnProperty(primaryKeyAttribute)) {
defaults[primaryKeyAttribute] = null;
}
});
} }
if (this.constructor._timestampAttributes.createdAt && defaults[this.constructor._timestampAttributes.createdAt]) { if (this.constructor._timestampAttributes.createdAt && defaults[this.constructor._timestampAttributes.createdAt]) {
......
...@@ -136,9 +136,14 @@ describe(Support.getTestDialectTeaser('Instance'), () => { ...@@ -136,9 +136,14 @@ describe(Support.getTestDialectTeaser('Instance'), () => {
expect(isUUID(user.uuidv4, 4)).to.be.true; expect(isUUID(user.uuidv4, 4)).to.be.true;
}); });
it('should store a valid uuid if the field is a primary key named id', function() { it('should store a valid uuid if the multiple primary key fields used', function() {
const Person = this.sequelize.define('Person', { const Person = this.sequelize.define('Person', {
id: { id1: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV1,
primaryKey: true
},
id2: {
type: DataTypes.UUID, type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV1, defaultValue: DataTypes.UUIDV1,
primaryKey: true primaryKey: true
...@@ -146,8 +151,11 @@ describe(Support.getTestDialectTeaser('Instance'), () => { ...@@ -146,8 +151,11 @@ describe(Support.getTestDialectTeaser('Instance'), () => {
}); });
const person = Person.build({}); const person = Person.build({});
expect(person.id).to.be.ok; expect(person.id1).to.be.ok;
expect(person.id).to.have.length(36); expect(person.id1).to.have.length(36);
expect(person.id2).to.be.ok;
expect(person.id2).to.have.length(36);
}); });
}); });
describe('current date', () => { describe('current date', () => {
......
...@@ -586,9 +586,14 @@ describe(Support.getTestDialectTeaser('Model'), () => { ...@@ -586,9 +586,14 @@ describe(Support.getTestDialectTeaser('Model'), () => {
}); });
describe('create', () => { describe('create', () => {
it('works with non-integer primary keys with a default value', function() { it('works with multiple non-integer primary keys with a default value', function() {
const User = this.sequelize.define('User', { const User = this.sequelize.define('User', {
'id': { 'id1': {
primaryKey: true,
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4
},
'id2': {
primaryKey: true, primaryKey: true,
type: DataTypes.UUID, type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4 defaultValue: DataTypes.UUIDV4
...@@ -602,7 +607,8 @@ describe(Support.getTestDialectTeaser('Model'), () => { ...@@ -602,7 +607,8 @@ describe(Support.getTestDialectTeaser('Model'), () => {
return this.sequelize.sync({ force: true }).then(() => { return this.sequelize.sync({ force: true }).then(() => {
return User.create({}).then(user => { return User.create({}).then(user => {
expect(user).to.be.ok; expect(user).to.be.ok;
expect(user.id).to.be.ok; expect(user.id1).to.be.ok;
expect(user.id2).to.be.ok;
}); });
}); });
}); });
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!