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

Most upvoted comments

hey @pleerock ,

it is true, that sqlite / cordova does not support boolean types, but rather fallback to tinyint… However, as @robmarti states, inserting an object, where you set pinned : true works fine (i.e., it is mapped correctly to 1; or 0 if you use false). If you want to query the same entry via repository.find( { pinned : true } ); however, it returns nothing, as there is no true in the database, but rather 1.

So the question would be, how to use boolean type in the model definition, but correctly map it to tinyint in the sqlite / cordova layer. So one can safely use the models (where respective fields are typed as boolean)…

Hope this helps!? All the best and cheers!