rails boolean and mysql tinyint(1) question?

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

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

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

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

thx, VSG