2015-05-14 49 views
0
PG::UndefinedColumn at /books/17 
ERROR: column "books_count" does not exist at character 45 
STATEMENT: UPDATE "users" SET "books_count" = COALESCE("books_count", 0) - 1 WHERE "users"."id" = $1 

在rails中更改運行名稱更改後,出現上述錯誤。PG :: UndefinedColumn在Rails重命名後列

def change 
    rename_column :users, :books_count, :books_shared 
    end 

我在試圖刪除一本書時首先注意到了這個問題。否則,一切正常。 Postgresql中是否還有一列需要更改?或者我應該恢復名稱更改?

我已經重新啓動服務器。

編輯:當前架構

create_table "users", force: :cascade do |t| 
    t.string "email",     default: "", null: false 
    t.string "encrypted_password",  default: "", null: false 
    t.string "reset_password_token" 
    t.datetime "reset_password_sent_at" 
    t.datetime "remember_created_at" 
    t.integer "sign_in_count",   default: 0, null: false 
    t.datetime "current_sign_in_at" 
    t.datetime "last_sign_in_at" 
    t.inet  "current_sign_in_ip" 
    t.inet  "last_sign_in_ip" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    t.string "name" 
    t.integer "books_shared",   default: 0 
    t.integer "books_read",    default: 0 
    end 

books_shared WAS books_count。 books_count

協會

class Book < ActiveRecord::Base 
     belongs_to :user, counter_cache: true 
     validates :title, presence: true, length: {minimum: 3} 
     validates :author, presence: true 
     has_many :book_checkouts 
    end 
+1

http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-belongs_to

您可以爲您的圖書類counter_cache像這樣指定自定義列名你有一個:books_count列出現在你的數據庫?你也可以複製粘貼你的表格關聯到你的模型中。 – Cyzanfar

+0

遷移後,我搜索了整個應用程序。沒有登錄books_count的跡象 – goda

+0

好吧,您已將您的表名從books_count更改爲books_share,也許這就是爲什麼。您還沒有在模型中發佈您的關聯。 – Cyzanfar

回答

0

默認情況下Rails將針對使用複數相關型號後面加_count,而你的情況是books_count的其他實例。

着眼於文檔的belongs_to這裏尋找counter_cache

belongs_to :user, counter_cache: :books_shared

相關問題