开发者

Return value diff between Postgres and MySql for booleans

If I do this:

ActiveRecord::Base.connection.execute('select 1 as t').first

on Postgres and MySql I get this:

MySql: [开发者_如何学C1]
Postgres: {"t"=>"1"}

Can someone explain what that is about?!


Regardless of where the bug is, this shows an interesting difference between the two databases (and an example of why cross-db abstraction is hard).

MySQL has no true bool type as such. It merely uses tinyints as boolean values.

On MySQL:

SELECT true;
+------+
| TRUE |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

Boolean expressions also evaluate to integers:

select true is not false;
+-------------------+
| true is not false |
+-------------------+
|                 1 |
+-------------------+
1 row in set (0.00 sec)

On PostgreSQL

PostgreSQL supports a true boolean type with either a 't' or an 'f' returned.

select true;
 bool 
------
 t
(1 row)

select true is not false;
 ?column? 
----------
 t
(1 row)

Note you can also cast ints to bool:

select 1::bool;
 bool 
------
 t
(1 row)

select 2::bool;
 bool 
------
 t
(1 row)

select 0::bool;
 bool 
------
 f
(1 row)

Evidently the two authors of these packages had different ideas of how to address the question of boolean types.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜