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

Commit c28aef7a by Jan Aagaard Meier

Support for bit fields as boolean storage. Closes #1896

1 parent 6fa8afe9
Showing with 38 additions and 0 deletions
...@@ -305,6 +305,10 @@ module.exports = (function() { ...@@ -305,6 +305,10 @@ module.exports = (function() {
// Convert boolean-ish values to booleans // Convert boolean-ish values to booleans
if (this.Model._hasBooleanAttributes && this.Model._isBooleanAttribute(key) && value !== null && value !== undefined && !value._isSequelizeMethod) { if (this.Model._hasBooleanAttributes && this.Model._isBooleanAttribute(key) && value !== null && value !== undefined && !value._isSequelizeMethod) {
if (Buffer.isBuffer(value) && value.length === 1) {
// Bit fields are returned as buffers
value = value[0];
}
value = !!value; value = !!value;
} }
......
...@@ -73,6 +73,40 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -73,6 +73,40 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}) })
}) })
if (Support.dialectIsMySQL()) {
// Bit fields interpreted as boolean need conversion from buffer / bool.
// Sqlite returns the inserted value as is, and postgres really should the built in bool type instead
it('allows bit fields as booleans', function () {
var self = this,
bitUser = this.sequelize.define('bituser', {
bool: 'BIT(1)'
}, {
timestamps: false
});
// First use a custom data type def to create the bit field
return bitUser.sync({ force: true }).then(function () {
// Then change the definition to BOOLEAN
bitUser = self.sequelize.define('bituser', {
bool: DataTypes.BOOLEAN
}, {
timestamps: false
});
return bitUser.bulkCreate([
{ bool: 0 },
{ bool: 1 }
]);
}).then(function () {
return bitUser.findAll();
}).then(function (bitUsers) {
expect(bitUsers[0].bool).not.to.be.ok;
expect(bitUsers[1].bool).to.be.ok;
});
});
}
it('does not modify the passed arguments', function (done) { it('does not modify the passed arguments', function (done) {
var options = { where: ['specialkey = ?', 'awesome']} var options = { where: ['specialkey = ?', 'awesome']}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!