簡體   English   中英

ActiveRecord :: StatementInvalid SQLite3 :: SQLException:沒有這樣的列:true:

[英]ActiveRecord::StatementInvalid SQLite3::SQLException: no such column: true:

我希望@messages返回@folder.messages,其中“已刪除”列的值不等於true。 我不確定為什么這會繼續拋出SQLException。 我想我沒有正確格式化已刪除的屬性,但我不確定如何解決它。

任何幫助將不勝感激。 提前致謝。

錯誤信息:

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

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沒有單獨的布爾存儲類。 相反,布爾值存儲為整數0(假)和1(真)。

所以,當你這樣說時:

deleted != true

SQLite不知道什么是true ,所以它假設你試圖引用另一個列名。

處理這個問題的正確方法是讓AR將你的Ruby布爾值轉換為SQLite布爾值(如Tam和fl00r的答案)。 我認為知道你做錯了什么是有用的。

更新 :如果你想檢查非真實deleted並包含NULL,那么你會想要這個:

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

或者更好的是,根本不允許deleted NULL。 你不應該允許NULL是任何列,除非你絕對必須(ActiveRecord的可空性默認與它應該是完全相反)。 SQL NULL是一個奇怪的野獸,你總是要特別對待它,除非你需要一個列的“not there”或“unspecified”值,否則最好不要允許它。

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

true對於不同的數據庫是不同的。 在某些情況下它是t/f值,並且在某些true/false ,所以你應該將它放在引號中並確定它是否適合你的特定數據庫,或者你應該將它從你的sql中排除,所以Rails會這樣做為你工作。

UPD

如果deletedNULL 第一。 默認情況下,將刪除的字段設置為false 二,如何用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)

嘗試

@ms = @msgs.where(["deleted != ?",true])


[英]#<ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: users.sender_id:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM