New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add three-state boolean problem #275
Conversation
a3397ca
to
ab800ff
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
Added a couple of cosmetic notes.
ab800ff
to
c002434
Compare
Thanks @pirj I have made the suggested changes. |
c002434
to
371d044
Compare
README.adoc
Outdated
With SQL databases, if a boolean column is not given a default value, it ends up having three possible values: `true`, `false` and `NULL`. | ||
Boolean operators [work in unexpected ways](https://en.wikipedia.org/wiki/Three-valued_logic) with `NULL`. | ||
|
||
E.g. in SQL queries, `true AND NULL` is `NULL` (not false), `true AND NULL OR false` is `NULL` (not false). This can make SQL queries return unexpected results. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor, but starting a sentence with "E.g." feels awkward. How about "For example"?
README.adoc
Outdated
@@ -1026,6 +1026,24 @@ end | |||
While enforcing table defaults only in Rails is suggested by many Rails developers, it's an extremely brittle approach that leaves your data vulnerable to many application bugs. | |||
And you'll have to consider the fact that most non-trivial apps share a database with other applications, so imposing data integrity from the Rails app is impossible. | |||
|
|||
=== 3-state Boolean [[three-state-boolean]] | |||
|
|||
With SQL databases, if a boolean column is not given a default value, it ends up having three possible values: `true`, `false` and `NULL`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think "will have" reads better than "ends up having".
README.adoc
Outdated
|
||
E.g. in SQL queries, `true AND NULL` is `NULL` (not false), `true AND NULL OR false` is `NULL` (not false). This can make SQL queries return unexpected results. | ||
|
||
To avoid such situations, boolean columns should always have a default value and `NOT NULL` constraint. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"and a NOT NULL
"
add_column :users, :active, :boolean | ||
|
||
# good - boolean with a default value and with restricted NULL | ||
add_column :users, :active, :boolean, default: true, null: false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer an example code of the rule that defaults to false
here :-) Of course, In practice, false
or true
is chosen depending on the context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or I think one approach is to exemplify an example with default: false
and an example with default: true
.
README.adoc
Outdated
# bad - boolean without a default value | ||
add_column :users, :active, :boolean | ||
|
||
# good - boolean with a default value and with restricted NULL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# good - boolean with a default value and with restricted NULL | |
# good - boolean with a default value (`false` or `true`) and with restricted NULL |
371d044
to
b329f73
Compare
Thanks! |
Issue: rubocop/rubocop-rails#337
Ref: https://thoughtbot.com/blog/avoid-the-threestate-boolean-problem