ActiveRecord::StatementInvalid SQLite3::SQLException: no such column: true:

前端 未结 3 1056
无人及你
无人及你 2020-12-31 21:20

I want to have @messages return @folder.messages where the value of column \"deleted\" is NOT equal to true. I\'m not sure why this keeps throwing a SQLException. I guess

3条回答
  •  离开以前
    2020-12-31 22:12

    SQLite uses C-style boolean values:

    SQLite does not have a separate Boolean storage class. Instead, Boolean values are stored as integers 0 (false) and 1 (true).

    So, when you say this:

    deleted != true
    

    SQLite doesn't know what true is so it assumes you're trying to reference another column name.

    The proper way to deal with this is to let AR convert your Ruby boolean to an SQLite boolean (as in Tam's and fl00r's answers). I think it is useful to know what you're doing wrong though.

    UPDATE: If you want to check for non-true deleted and include NULL then you'll want this:

    @ms = @msgs.where("deleted != ? OR deleted IS NULL", true)
    

    Or better, don't allow NULLs in deleted at all. You shouldn't allow NULL is any column unless you absolutely have to (ActiveRecord's default for nullability is exactly the opposite of what it should be). The SQL NULL is an odd beast and you always have to treat it specially, best not to allow it unless you need a "not there" or "unspecified" value for a column.

提交回复
热议问题