簡體   English   中英

帶有Rails的敏捷Web開發-ActiveRecord :: StatementInvalid錯誤

[英]Agile Web Development with Rails - ActiveRecord::StatementInvalid Error

我是Rails的新手,已經開始通過“使用Rails進行敏捷Web開發:第四版”進行工作。 我目前在購物車創建一章。 我大部分都是從本書中復制代碼的,但是我的數據庫稱為“ dvds”而不是“ products”(因為我正在為項目創建DVD商店)。

我到了本章的結尾,當我運行功能測試時,出現以下錯誤:

test_should_destroy_dvd(DvdsControllerTest):
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: line_items.dvd_id: SELECT COUNT(*) FROM "line_items" WHERE ("line_items".dvd_id = 980190962)
app/models/dvd.rb:26:in `ensure_not_referenced_by_any_line_item'
app/controllers/dvds_controller.rb:76:in `destroy'
test/functional/dvds_controller_test.rb:50:in `test_should_destroy_dvd'
test/functional/dvds_controller_test.rb:49:in `test_should_destroy_dvd'

這是dvd.rb文件所引用的代碼:

has_many :line_items 
before_destroy :ensure_not_referenced_by_any_line_item
...
private
  def ensure_not_referenced_by_any_line_item
if line_items.empty?
  return true
else
  errors.add(:base, 'Line Items present')
  return false
 end
end

dvds_controller.rb中的代碼:

def destroy
    @dvd = Dvd.find(params[:id])
    @dvd.destroy

    respond_to do |format|
      format.html { redirect_to(dvds_url) }
      format.xml  { head :ok }
    end
  end

以及來自dvds_controller_test.rb的代碼:

test "should destroy dvd" do
    assert_difference('Dvd.count', -1) do
      delete :destroy, :id => @dvd.to_param
    end

    assert_redirected_to dvds_path
  end

本質上,我不理解該錯誤,我正在尋找幫助,以使其消失! 我猜問題出在“沒有這樣的列:line_items.dvd_id”,但我不知道如何修改它。 我已經竭盡全力試圖解決這個問題,因此對您的任何幫助將不勝感激。

編輯:這似乎是問題所在,在create_line_items.rb中:

class CreateLineItems < ActiveRecord::Migration
  def self.up
    create_table :line_items do |t|
      t.integer :product_id
      t.integer :cart_id

      t.timestamps
    end
  end

  def self.down
    drop_table :line_items
  end
end 

另外,這是場景:

ActiveRecord::Schema.define(:version => 20111125123848) do

  create_table "carts", :force => true do |t|
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "dvds", :force => true do |t|
    t.string   "title"
    t.text     "description"
    t.string   "image_url"
    t.decimal  "price",       :precision => 8, :scale => 2
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "line_items", :force => true do |t|
    t.integer  "product_id"
    t.integer  "cart_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

end

確保運行rake db:migrate創建數據庫。

就像Cat所說的那樣,請確保您運行了遷移,但還要注意,您可能忘記了在遷移中更改列名。 我的猜測是過去是product_id。 如果您還發布了與LineItem和當前db / schema.rb相關的遷移信息,將很有幫助。

我認為該架構看起來不錯,遷移缺少dvd_id。 您如何在模式中以dvd_id結尾? 您是否有其他遷移添加了它?

可能值得檢查一下數據庫中的實際內容:

sqlite3 db/development.sqlite3

sqlite3> .schema line_items

檢查line_items是否確實具有dvd_id列

暫無
暫無

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

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