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

Commit 48a861d4 by Sascha Depold

Merge pull request #857 from durango/enum-null

Enum values can now be null
2 parents d633069a 202aeedd
...@@ -608,7 +608,7 @@ module.exports = (function() { ...@@ -608,7 +608,7 @@ module.exports = (function() {
, hasValue = (typeof values[attrName] !== 'undefined') , hasValue = (typeof values[attrName] !== 'undefined')
, valueOutOfScope = ((definition.values || []).indexOf(values[attrName]) === -1) , valueOutOfScope = ((definition.values || []).indexOf(values[attrName]) === -1)
if (isEnum && hasValue && valueOutOfScope) { if (isEnum && hasValue && valueOutOfScope && !(definition.allowNull === true && values[attrName] === null)) {
throw new Error('Value "' + values[attrName] + '" for ENUM ' + attrName + ' is out of allowed scope. Allowed values: ' + definition.values.join(', ')) throw new Error('Value "' + values[attrName] + '" for ENUM ' + attrName + ' is out of allowed scope. Allowed values: ' + definition.values.join(', '))
} }
} }
......
...@@ -152,7 +152,7 @@ module.exports = (function() { ...@@ -152,7 +152,7 @@ module.exports = (function() {
valueOutOfScope = ((definition.values || []).indexOf(values[attrName]) === -1) valueOutOfScope = ((definition.values || []).indexOf(values[attrName]) === -1)
} }
if (isEnum && hasValue && valueOutOfScope) { if (isEnum && hasValue && valueOutOfScope && !(definition.allowNull === true && values[attrName] === null)) {
throw new Error('Value "' + values[attrName] + '" for ENUM ' + attrName + ' is out of allowed scope. Allowed values: ' + definition.values.join(', ')) throw new Error('Value "' + values[attrName] + '" for ENUM ' + attrName + ' is out of allowed scope. Allowed values: ' + definition.values.join(', '))
} }
......
...@@ -593,6 +593,23 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -593,6 +593,23 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}) })
}) })
}) })
it('allows null values', function(done) {
var Enum = this.sequelize.define('Enum', {
state: {
type: Sequelize.ENUM,
values: ['happy', 'sad'],
allowNull: true
}
})
Enum.sync({ force: true }).success(function() {
Enum.create({state: null}).success(function(_enum) {
expect(_enum.state).to.be.null
done()
})
})
})
}) })
}) })
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!