diff --git a/src/bone.js b/src/bone.js index adee02b1..80eaf46f 100644 --- a/src/bone.js +++ b/src/bone.js @@ -717,6 +717,7 @@ class Bone { for (const name in attributes) { const value = this.attribute(name); const { defaultValue } = attributes[name]; + // console.log(attributes[name], name, defaultValue); if (value != null) { data[name] = value; } else if (value === undefined && defaultValue != null) { diff --git a/src/drivers/abstract/attribute.js b/src/drivers/abstract/attribute.js index 18dd95b8..a942ea06 100644 --- a/src/drivers/abstract/attribute.js +++ b/src/drivers/abstract/attribute.js @@ -99,15 +99,23 @@ class Attribute { } const type = createType(DataTypes, params); const dataType = params.dataType || type.dataType; + let { defaultValue = null } = params; + try { + // normalize column defaults like `'0'` or `CURRENT_TIMESTAMP` + defaultValue = type.cast(type.uncast(defaultValue)); + } catch { + defaultValue = null; + } + Object.assign(this, { name, columnName, primaryKey: false, - defaultValue: null, allowNull: !params.primaryKey, columnType: type.toSqlString().toLowerCase(), ...params, type, + defaultValue, dataType, jsType: findJsType(DataTypes, type, dataType), }); diff --git a/test/dumpfile.sql b/test/dumpfile.sql index 77ee5d06..0120d2e9 100644 --- a/test/dumpfile.sql +++ b/test/dumpfile.sql @@ -72,6 +72,7 @@ DROP TABLE IF EXISTS `likes`; CREATE TABLE `likes` ( `id` bigint(20) AUTO_INCREMENT PRIMARY KEY, `gmt_create` timestamp(3) NULL, + `gmt_modified` timestamp DEFAULT CURRENT_TIMESTAMP, `article_id` bigint(20) NOT NULL, `user_id` bigint(20) NOT NULL, `gmt_deleted` timestamp(3) NULL diff --git a/test/unit/adapters/sequelize.test.js b/test/unit/adapters/sequelize.test.js index 1bab018c..a3b46d7a 100644 --- a/test/unit/adapters/sequelize.test.js +++ b/test/unit/adapters/sequelize.test.js @@ -1917,7 +1917,7 @@ describe('Transaction', function() { describe('mysql only', () => { const Spine = sequelize(Bone); - + class Post extends Spine { static get table() { return 'articles'; @@ -1940,9 +1940,9 @@ describe('mysql only', () => { }); describe('Model.update with order, limit (mysql only)', () => { - + it('should work', async () => { - + let i = 0; while (i <= 5) { await Post.create({ title: 'Throne' }); @@ -1959,7 +1959,7 @@ describe('mysql only', () => { assert.equal(allPosts[1].title, 'Game'); assert.equal(allPosts[2].title, 'Throne'); assert.equal(allPosts[3].title, 'Throne'); - + await Post.bulkUpdate({ title: 'Pilot' }, { where: {}, limit: 2, @@ -1976,9 +1976,9 @@ describe('mysql only', () => { }); describe('Model.destroy with order, limit (mysql only)', () => { - + it('should work', async () => { - + let i = 0; const posts = []; while (i <= 5) { diff --git a/test/unit/drivers/mysql/attribute.test.js b/test/unit/drivers/mysql/attribute.test.js index 52e1bb7f..6a0ec433 100644 --- a/test/unit/drivers/mysql/attribute.test.js +++ b/test/unit/drivers/mysql/attribute.test.js @@ -2,11 +2,11 @@ const assert = require('assert').strict; const Attribute = require('../../../../src/drivers/mysql/attribute'); -const { BOOLEAN, JSONB } = Attribute.DataTypes; +const { BOOLEAN, DATE, JSONB } = Attribute.DataTypes; describe('=> Attribute (mysql)', function() { it('should support TINYINT(1)', async function() { - const attribute= new Attribute('has_image', { + const attribute = new Attribute('has_image', { type: BOOLEAN, defaultValue: false, }); @@ -17,4 +17,12 @@ describe('=> Attribute (mysql)', function() { const attribute = new Attribute('params', { type: JSONB }); assert.equal(attribute.toSqlString(), '`params` JSON'); }); + + it('should normalize attribute defaultValue', async function() { + const attribute = new Attribute('createdAt', { + type: DATE, + defaultValue: 'CURRENT_TIMESTAMP', + }); + assert.equal(attribute.defaultValue, null); + }); });