2016-03-12 19 views
0

我使用find_by_id檢索Post實例,在其上設置分數並調用保存。我看到的是一個非常奇怪的錯誤,其中SQL缺少WHERE子句中的字段。這裏是我的鐵軌控制檯輸出:模型保存方法導致異常「沒有這樣的列」列名爲空

me> rails c 
Loading development environment (Rails 3.2.22.2) 
2.1.5 :001 > post = Post.find_by_id 123456 
    Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = 123456 LIMIT 1 
=> #<Post id: 123456, url: "http://www.example.com", title: "Test Title", score: nil> 
2.1.5 :002 > post.score = 1 
=> 1 
2.1.5 :003 > post.save 
    (0.1ms) begin transaction 
    (0.2ms) UPDATE "posts" SET "score" = 1 WHERE "posts"."" = 123456 
    (0.0ms) rollback transaction 
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: posts.: UPDATE "posts" SET "score" = 1 WHERE "posts"."" = 123456 
+0

缺少ID的字段。你在這裏做什麼奇怪的事情?像在遷移中爲字段設置數據庫選項一樣?你可以粘貼你的帖子遷移? – trh

+0

@trh是的,我有':id => false'在create_table for:posts – slowpoison

回答

0

的問題是,我的架構定義posts

create_table "posts", :id => false, :force => true do |t| 
    t.integer "id",  :limit => 8 
    ... 

這意味着Rails的奉命沒有主鍵列。我所要做的只是添加

set_primary_key :id 

到我的模型。

相關問題