typeorm: [Bug] Boolean values not casted properly when used in .find() condition
Issue type:
[ ] question [x] bug report [ ] feature request [ ] documentation issue
Database system/driver:
[x] cordova
[ ] mongodb
[ ] mssql
[ ] mysql / mariadb
[ ] oracle
[ ] postgres
[ ] sqlite
[ ] sqljs
[ ] react-native
TypeORM version:
[x] latest
[ ] @next
[x] 0.2.0 (or put your version here)
Dear @pleerock and community,
before describing my actual problem, I want to thank you for this awesome project 👍
When developing a mobile application with Ionic (Cordova, respectively), I stumbled upon the following issue. Consider the following example:
I have a Product model, that looks as follows:
@Entity()
export class Product {
// various columns here
@Column()
liked: boolean;
}
and a respective ProductRepository in order to handle access to this model. This works so far, and i can create / update / delete models fine.
However, if i want to show only the Products that are liked, I request data like this:
// repository is the ProductRepository!
products = await repository.find({
liked : true
});
However, when executing this query, i get an empty result. Problem is, that the database used is a SQLite database (remember that i use Ionic!). Connection is set up properly (remember that I can save / update / delete models respectively). The Boolean attribute is internally represented as an integer in the SQLite database. When I use 1 instead of true, however, it works.
// repository is the ProductRepository!
products = await repository.find({
liked : 1
});
How can i properly “configure” the ORM to use boolean values instead of tinyint for the find() calls?
Thank you very much for your help, really appreciate it! Cheers
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 1
- Comments: 18 (18 by maintainers)
Commits related to this issue
- added test for #1981 — committed to typeorm/typeorm by deleted user 6 years ago
- working "github issues > #1981 Boolean values not casted properly when used in .find() condition" — committed to typeorm/typeorm by bbakhrom 5 years ago
- fix: sqlite boolean parameter escape sqlite does not support boolean parameters. Even though sqlite is able to transform true to 1 and false to 0 there might be some limitations with other implemenat... — committed to michaelwolz/typeorm by michaelwolz 2 years ago
- fix: boolean parameter escape in SQLiteDriver (#9400) * fix: sqlite boolean parameter escape sqlite does not support boolean parameters. Even though sqlite is able to transform true to 1 and false... — committed to typeorm/typeorm by michaelwolz 2 years ago
hey @pleerock ,
it is true, that sqlite / cordova does not support
booleantypes, but rather fallback totinyint… However, as @robmarti states, inserting an object, where you setpinned : trueworks fine (i.e., it is mapped correctly to1; or0if you usefalse). If you want to query the same entry viarepository.find( { pinned : true } );however, it returns nothing, as there is notruein the database, but rather1.So the question would be, how to use
booleantype in the model definition, but correctly map it totinyintin the sqlite / cordova layer. So one can safely use the models (where respective fields are typed asboolean)…Hope this helps!? All the best and cheers!