I have a question about Booleans. I am trying to i...
# cfml-general
o
I have a question about Booleans. I am trying to insert a Boolean value into my MySQL database and am getting the error
Incorrect integer value: 'true' for column 'notifications' at row 1
. Inspecting my database it shows my column is a tinyint. Doing some research MySQL does not have support for true Booleans, however it does support
true, false, True, False, TRUE, FALSE
values which is what I am doing. Is there something going on behind the scenes? Source: https://www.mysqltutorial.org/mysql-boolean/
e
It depends on how you are trying to insert it, but in general, given tinyint, I would send '1' for true and '0' for false. You are also allowing for NULLS, so it could also be 'populated' that way by omitting the field upon write
Are you using cfsqlparam?
o
no regular cfml
Copy code
var user = getInstance( "User" )
			.firstWhere( "id", auth.user().getId() ?: -1 )
			.setNotifications( true )
            .save();
e
Oh, right, fair enough - so what type of SQL var is yours - also tinyint?
(notifications is the var I assume)
Okay, then I would change your submission value to '1' for true or '0' for false
o
Fair enough, though why it says it supports
true
and
false
and it doesn't is beyond me.
Thanks
e
It depends on where you're reading it, and what it applies to - for instance maybe it means that inside a SQL statement you can proxy use 'true' for 1, etc, but at the data storage level (which I bet you are accessing more or less directly in this instance) the boolean data type in MySQL can only be tinyint
If substituting the numbers 1 and 0 don't work, then I'll also be curious what the answer is!
s
@Ookma-Kyi I can conform that the underlying MySQL JDBC driver absolutely allows you to pass Booleans into
BIT(1)
fields which is what we use for representing Booleans -- but both ACF and Lucee seem to translate this to 0/1 on read and for whatever weird reason require 0/1 on write as well. It's bonkers. CFML has a very warped view of Boolean, unfortunately.
a
NB: the error talks about
'true'
(a string) not
true
(a boolean literal). Are you sure whatever DB-abstraction library you are using isn't somehow deciding
.setNotifications( true )
should be using the string
"true"
? Seems unlikely, that said. BTW you describe this as "regular CFML":
Copy code
var user = getInstance( "User" )
			.firstWhere( "id", auth.user().getId() ?: -1 )
			.setNotifications( true )
            .save();
That is some DB abstraction library in play. It's not "regular CFML" (which would just be a
queryExecute
call). They error should emit the exact SQL that was rejected by the DB... would be helpful to post that.
s
I suspect it's
qb
or maybe ORMBox (at first I thought cfWheels but that's all
getModel()
nonsense isn't it?)
a
It's not CFWheels. CFWheel's "ORM" basically has one writing SQL in one's function args to do anything useful. It's not an ORM. It's not even a good DBAL. It's one of those things that shows that "just writing the SQL" is oftentimes easier and more efficient than using some intermediate-grade CF devs' idea of "not having to write SQL any more we'll call it an ORM": • simple SQL is easier and clearer to do in SQL than their "special way"; • non-obvious stuff basically requires one to write SQL in the function calls anyhow; • anything complicated just needs SQL. QB looks like a work of Michelangelo, comparatively.
s
LOL! Don't get me started -- you know how I feel about ORMs... 🔥
a
BTW,
getModel
is far too much "good naming" for CFWheels. In CFWheels it's just
model
(which I guess is also a verb so could be construed as a method name I guess, but then one sees the equivalent function
controller
and realise it's just bad naming).