Skip to content

Commit 9da0864

Browse files
getappsodabrew
authored andcommitted
Add a note about casting boolean columns (brianmario#770)
Explains why "SELECT true" doesn't get converted to a boolean with cast_booleans: true.
1 parent f16b3b0 commit 9da0864

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,15 @@ client = Mysql2::Client.new
399399
result = client.query("SELECT * FROM table_with_boolean_field", :cast_booleans => true)
400400
```
401401

402+
Keep in mind that this works only with fields and not with computed values, e.g. this result will contain `1`, not `true`:
403+
404+
``` ruby
405+
client = Mysql2::Client.new
406+
result = client.query("SELECT true", :cast_booleans => true)
407+
```
408+
409+
CAST function wouldn't help here as there's no way to cast to TINYINT(1). Apparently the only way to solve this is to use a stored procedure with return type set to TINYINT(1).
410+
402411
### Skipping casting
403412

404413
Mysql2 casting is fast, but not as fast as not casting data. In rare cases where typecasting is not needed, it will be faster to disable it by providing :cast => false. (Note that :cast => false overrides :cast_booleans => true.)

0 commit comments

Comments
 (0)