2017-05-24 2 views
0

、私はこのエラーを取得:ターミナルではActiveRecord :: InvalidForeignKeyがエラー

ActiveRecord::InvalidForeignKey in InventoriesController#destroy 

SQLite3::ConstraintException: FOREIGN KEY constraint failed: DELETE FROM "inventories" WHERE "inventories"."id" = ? 

を、それが

{"_method"=>"delete", "authenticity_token"=>"dBNU2GkV0+rOcp4NVEljm4oIpkdOnPsvZKdmisaadBzX3QkY1VwurZNRPL0WFtVvizeAcJb7H6E50ObmpRsXAg==", "id"=>"1"} 

を言うことも原因であると言います時:私の在庫で

def destroy 
    @inventory = Inventory.find(params[:id]) 
    @inventory.destroy 

    redirect_to inventory_path 
end 

あるファイル:

class InventoriesController < ApplicationController 
    def show 
     @inventory = Inventory.find(params[:id]) 
    end 

    def index 
     @inventories = Inventory.all 
    end 

    def new 
     @inventory = Inventory.new 
    end 

    def create 
     @inventory = Inventory.new(inventory_params) 


     if @inventory.save 
     redirect_to @inventory 
     else 
      render 'new' 
     end 
    end 

    def edit 
     @inventory = Inventory.find(params[:id]) 
    end 

    def update 
     @inventory = Inventory.find(params[:id]) 
     if @inventory.update(inventory_params) 
      redirect_to @inventory 
     else 
      render 'edit' 
     end 
    end 

    def destroy 
     @inventory = Inventory.find(params[:id]) 
     @inventory.destroy 

     redirect_to inventory_path 
    end 
end 

private 
def inventory_params 
    params.require(:inventory).permit(:product_name, :brand_name, :item_id, :upc_code, :color, :department, :size, :condition, :fabric_type, :shipping_weight, :sku, :asin, :quantity, :cost_price, :sell_price, :key_product_features, :product_description, :search_terms, :status, :listing_in_usa, :listing_in_canada, :listing_in_mexico) 

end 
+0

あなたは 'Inventory'と' Item'の間に関係があるので、それに対処したり、あなたの関係の中に 'destroy''依存関係を指定する必要があります。 –

答えて

1

インベントリモデルの外部キーがあり、インベントリモデルでhas_manyまたはhas_oneという関係で表示されることが予想される別のキーのようです。

子レコードを自動的に削除するように構成された外部キーを使用するか、関連付けにdependent: :destroyを指定するかのいずれかの修正が行われます。

前者は非常に高速ですが、子インスタンスのコールバックを実行できないため、:destroyオプションをお勧めします。

関連する問題