-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
toJSON converting booleans to "t" (true) or "f" (false) #8749
Comments
I'm seeing this same issue, but with 4.26.0. @jonahlau Did you mean 4.26.0 as well? You had marked yes to tested with latest. Downgrading to 4.25.2 fixed the issue for me. |
@karn09 Ah, yes sorry for the typo. I did mean 4.26.0 Downgrading to 4.25.2 fixed it for me. So should I avoid upgrading to latest release until this is resolved? |
@jonahlau until it's fixed probably a good idea, unless you need the fixes in the newer version more then proper booleans on toJSON. |
I seeing same issue with |
4.28.0 still has this issue. |
FYI I tried to replicate this issue but failed, #8791 (test case) If some one can add a reproducible test case I will take care of fixing it |
I don't know whether it's possible to have a test case for this. What I do know is that when you connect the latest sequelize releases to a postgres database, and query a model with a boolean column, the values come back as 't' and 'f'. The most recent release that correctly returns bools from postgres is 4.25.2. Perhaps something introduced with postgres support for enums broke the bool typecasting? With release 4.26.0 (and 4.28.6), I have a model
|
't' probably is what Postgre returns. There seems to be no conversion to JS boolean from the string value returned by Postgre. |
But you guys are facing this issue consistently on at least one machine, lets start with that. Please share your setup details (as much as possible) and answer these questions
|
Our system has some unit tests, which produce this issue every time when run with 4.28.6. I run the tests on my own machine. MacOS 10.13.2 (17C88) We do have some ENUMs defined, but not in the same table where the booleans do not work. |
Our system has some unit tests which produce this issue when run with 4.28.6. We run the tests within a docker-compose environment. Node image: node:8.9.0 To narrow down the test, I did the following in my environment: created a new model:
Created a mocha/chai test to verify behavior:
Output from mocha:
|
Same here
Even creating a new model with default valued boolean fields, it returns And downgrading to 4.25.2 fixed it temporarily. |
I noticed this problem when doing finds with I struggled to reproduce it in an isolated example, but your comments about ENUMs have helped me and I now have something that can reliably reproduce it for me. It only happens when I define an ENUM field. These are my versions:
'use strict';
const Sequelize = require('sequelize');
const Op = Sequelize.Op;
let Example;
/**
* @param {boolean} useEnum
*/
async function test(useEnum) {
const db = new Sequelize('test', 'user', 'user', {
host: 'postgres',
dialect: 'postgres',
operatorsAliases: false,
});
const attributes = {
s: { type: Sequelize.STRING, allowNull: false },
b: { type: Sequelize.BOOLEAN, allowNull: false },
};
if (useEnum) {
attributes.e = {
type: Sequelize.ENUM,
values: ['one', 'two', 'three'],
defaultValue: 'two',
};
}
const options = {
freezeTableName: true,
indexes: [
{ fields: ['s'], unique: true },
],
};
Example = db.define('example', attributes, options);
await db.sync({ force: true });
await populateDb();
await findOne();
await db.close();
}
async function populateDb() {
const e = await Example.create({ s: 'text', b: true });
logText('Create');
logModel(e);
}
async function findOne() {
const e1 = await Example.findOne({ where: { s: { [Op.eq]: 'text' } } });
logText('Normal find');
logModel(e1);
const e2 = await Example.findOne({ where: { s: { [Op.eq]: 'text' } }, raw: true });
logText('Raw find');
logModel(e2);
}
function logHeading() {
process.stdout.write("\x1b[34m");
console.log(...arguments);
process.stdout.write("\x1b[0m");
}
function logText() {
process.stdout.write("\x1b[32m");
console.log(...arguments);
process.stdout.write("\x1b[0m");
}
function logModel(e) {
process.stdout.write("\x1b[33m");
console.log(e.s, e.b, e.e);
process.stdout.write("\x1b[0m");
}
async function main() {
try {
logHeading('Model with no enums');
await test();
logHeading('Model with enums');
await test(true);
} catch (err) {
console.error(err);
process.exit(1);
}
}
main(); |
I'm facing the same problem @4.28.6 (postgres) when running the following.
the returned object has 't' or 'f' instead of booleans |
+1 Having this issue, our tests caught this.
Happens when using Can confirm that Sequelize |
Well done @sushantdhiman, the fix seems to work with my test cases! |
It works for me too. Thank you. |
What are you doing?
I have the user model as defined below.
What do you expect to happen?
toJSON should retain booleans and not convert them to strings
What is actually happening?
When I call toJSON of any instance of User in my CI tests (running in as a docker container with node 8.9.1), all booleans, for example fields
active
andisVerified
, get converted to "t" or "f".The problem doesn't happen when I run it on MacOS 10.12.15 running Node 8.9.1
Output, either JSON or SQL
Dialect: postgres
Dialect version: XXX
Database version: 9.5
Sequelize version: 4.2.0
Tested with latest release: yes (If yes, specify that version)
Note : Your issue may be ignored OR closed by maintainers if it's not tested against latest version OR does not follow issue template.
The text was updated successfully, but these errors were encountered: