Forum: Ruby on Rails rails boolean and mysql tinyint(1) question?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
VSG (Guest)
on 2009-03-23 23:58
(Received via mailing list)
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
Conrad T. (Guest)
on 2009-03-24 11:21
(Received via mailing list)
On Mon, Mar 23, 2009 at 2:06 PM, VSG <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
VSG (Guest)
on 2009-03-24 15:28
(Received via mailing list)
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
This topic is locked and can not be replied to.