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

扶醉桌前 提交于 2019-11-30 09:51:06

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.

@ms = @msgs.where("deleted != ?", true) 
# OR
@ms = @msgs.where(:deleted => false) 

true is different for different databases. In some it is t/f value, and in some true/false, so you should or place it in quotes and be sure if it is right for your particular database, or you should exclude it out of your sql so Rails will do the job for you.

UPD

If deleted is NULL. First. Set deleted field as a false by default. Second, how to find it with AR:

@ms = @msgs.where("deleted = ? OR deleted = ?", false, nil)
# wich won't work, Thanks to @mu is too short
@ms = @msgs.where("deleted = ? OR deleted IS NULL", false)

try

@ms = @msgs.where(["deleted != ?",true])
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!