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

user714001 picture user714001 · Apr 24, 2011 · Viewed 7.4k times · Source

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 I'm not formatting the deleted attribute properly, but I'm not sure how to fix it.

Any help would be greatly appreciated. Thanks in advance.

Error message:

ActiveRecord::StatementInvalid in MailboxController#index  
SQLite3::SQLException: no such column: true: SELECT     "message_copies".* FROM       "message_copies"  WHERE     ("message_copies".folder_id = 1) AND (deleted != true)  

Application Trace:

app/controllers/mailbox_controller.rb:14:in `show'  
app/controllers/mailbox_controller.rb:5:in `index'  

Mailbox_Controller.rb

1   class MailboxController < ApplicationController  
2     def index  
3       current_user = User.find(session[:user_id])  
4       @folder = Folder.where("user_id = #{current_user.id}").first  
5       show  
6       render :action => "show"  
7     end
8  
9     def show  
10      current_user = User.find(session[:user_id])  
11      @folder = Folder.where("user_id = #{current_user.id}").first  
12      @msgs = @folder.messages  
13      @ms = @msgs.where("deleted != true")  
14      @messages = @ms.all.paginate :per_page => 10,  
15                 :page => params[:page], :include => :message,  
16                 :order => "messages.created_at DESC"  
17    end  
18  end  

Answer

mu is too short picture mu is too short · Apr 24, 2011

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.