ActiveRecord :: StatementInvalid SQLite3 :: SQLException:否这样的列:true: [英] ActiveRecord::StatementInvalid SQLite3::SQLException: no such column: true:

查看:162
本文介绍了ActiveRecord :: StatementInvalid SQLite3 :: SQLException:否这样的列:true:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让@messages返回@ folder.messages,其中已删除"列的值不等于true.我不确定为什么这总是引发SQLException.我想我没有正确设置Deleted属性的格式,但是我不确定如何解决它.

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.

错误消息:

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)  

应用程序跟踪:

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

Mailbox_Controller.rb

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  

推荐答案

SQLite使用C样式布尔值:

SQLite uses C-style boolean values:

SQLite没有单独的布尔存储类.取而代之的是,布尔值存储为整数0(false)和1(true).

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不知道true是什么,因此它假定您正在尝试引用另一个列名.

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

处理此问题的正确方法是让AR将您的Ruby布尔值转换为SQLite布尔值(如Tam和fl00r的答案).我认为了解自己在做什么错很有用.

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.

更新:如果要检查非真实的deleted并包含NULL,则需要这样做:

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)

或者更好的是,完全不允许在deleted中使用NULL.除非绝对必要,否则您不应该允许NULL为任何列(ActiveRecord的可空性默认值与应为的完全相反). SQL NULL是一个奇怪的野兽,您始终必须对其进行特殊对待,最好不要允许它,除非您需要为列提供"not there"或"unspecified"值.

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.

这篇关于ActiveRecord :: StatementInvalid SQLite3 :: SQLException:否这样的列:true:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆