# Rails boolean and mysql tinyint(1) question?

**URL:** <https://www.ruby-forum.com/t/rails-boolean-and-mysql-tinyint-1-question/163686>\
**Category:** Rails\
**Created:** [March 23, 2009, 10:58pm UTC](https://www.ruby-forum.com/t/rails-boolean-and-mysql-tinyint-1-question/163686 "2009-03-23T22:58:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vsg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5ac83/32.png) [@vsg](https://www.ruby-forum.com/u/vsg)\
**Post date:** [March 23, 2009, 10:58pm UTC](https://www.ruby-forum.com/t/rails-boolean-and-mysql-tinyint-1-question/163686/1 "2009-03-23T22:58:54Z")

</div>

this may seem silly but i just noticed that if in mysql the column  
which is a tinyint(1) happens 2 or greater, by a chance of glitch or  
corruption in the database(i did it manually ofcourse but i am  
speaking hypothetically), rails would see it as false. but i find that  
really wrong seeing how in mysql it sees any non-zeroes as true.

i really think if it does happen to be any number other than 1 or 0 it  
should throw an exception saying the data is corrupted or something  
because rails should only return 0 or 1. am i crazy for thinking like  
this?

also can somebody point to me where in the code it does the tinyint(1)  
to boolean conversion? or could that possibly be a mysql thing?

thx,  
VSG

---

<div class="post-metadata">

**Author:** ![vsg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5ac83/32.png) [@vsg](https://www.ruby-forum.com/u/vsg)\
**Post date:** [March 24, 2009, 2:28pm UTC](https://www.ruby-forum.com/t/rails-boolean-and-mysql-tinyint-1-question/163686/2 "2009-03-24T14:28:28Z")

</div>

ahhhh thank you very much for the explanation. 🙂 what you have said  
makes perfect sense.

but wow i didnt think much about PostgreSQL. i need to read up about  
them.

thx,  
VSG

---

<div class="post-metadata">

**Author:** ![vsg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5ac83/32.png) [@vsg](https://www.ruby-forum.com/u/vsg)\
**Post date:** [March 24, 2009, 10:21am UTC](https://www.ruby-forum.com/t/rails-boolean-and-mysql-tinyint-1-question/163686/3 "2009-03-24T10:21:01Z")

</div>

On Mon, Mar 23, 2009 at 2:06 PM, VSG [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> this may seem silly but i just noticed that if in mysql the column  
> which is a tinyint(1) happens 2 or greater, by a chance of glitch or  
> corruption in the database(i did it manually ofcourse but i am  
> speaking hypothetically), rails would see it as false. but i find that  
> really wrong seeing how in mysql it sees any non-zeroes as true.

In regards to MySQL, tinyint( 1 ) represents the following range of  
values:

-128 to 127

Next, MySQL documents says the following which they have been saying for  
the  
longest:

“We intend to implement full boolean type handling, in accordance with  
standard SQL, in a future MySQL release.” 5.0

“We intend to implement full boolean type handling, in accordance with  
standard SQL, in a future MySQL release.” 5.1

“We intend to implement full boolean type handling, in accordance with  
standard SQL, in a future MySQL release.” 6.0

If you’re requiring better boolean handling, I would recommend switching  
to  
PostgreSQL. For example,

Valid literal values for the “true” state are:

TRUE’t’‘true’‘y’‘yes’‘1’

For the “false” state, the following values can be used:

FALSE’f’‘false’‘n’‘no’‘0’

Next, I really don’t want exceptions firing off because I inserted  
values  
into the database to  
test a hypothesis. You can probably do this for every type with MySQL.  
I  
would recommend  
sanitizing and/or validating your input before entering it into the  
database.

> i really think if it does happen to be any number other than 1 or 0 it  
> should throw an exception saying the data is corrupted or something  
> because rails should only return 0 or 1. am i crazy for thinking like  
> this?

Again, you should validate your input. If it’s a check box, the state  
is  
either 1 or 0 by definition.

> also can somebody point to me where in the code it does the tinyint(1)  
> to boolean conversion? or could that possibly be a mysql thing?

In short, Rails is following MySQL definition of what a boolean is and  
how  
it is to evaluated and you should be able to find the information in the  
following file within the Rails source:

rails/activerecord/lib/active\_record/connection\_adapters/mysql\_adapter.rb

Good luck,

-Conrad
