- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
当我尝试从我的库存数据库中删除某些内容时,出现此错误:
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
最佳答案
听起来好像有一个从另一个表到 inventory 表的外键,您希望在 Inventory 模型中看到它以 has_many
或 has_one
关系表示.
修复方法是将外键配置为自动删除子记录,或者在关联上指定 dependent::destroy
。
前者会非常快,但不允许在子实例上执行回调,所以我建议使用 :destroy
选项。
关于ruby-on-rails - ActiveRecord::InvalidForeignKey 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44164900/
我使用的是 mysql 5.5 和 Rails 3.2.13。我有这样的表: 'CREATE TABLE `visit` ( `id` bigint(20) NOT NULL AUTO_INCRE
当我尝试从我的库存数据库中删除某些内容时,出现此错误: ActiveRecord::InvalidForeignKey in InventoriesController#destroy SQLite3
在 ActiveAdmin 中,当我想删除用户时出现以下错误: ActiveRecord::InvalidForeignKey in Admin::UsersController#destroy PG
在我使用 postgresql 的 Rails 4 应用程序中,我遇到了一个我似乎无法理解的错误: Completed 500 Internal Server Error in 6ms (Active
ActiveRecord::InvalidForeignKey in ArticlesController#destroy SQLite3::ConstraintException: FOREIGN
我的模型: class Foo < ActiveRecord::Base has_many :bars, inverse_of: :foo accepts_nested_attributes_
我有一个Book 模型和一个User 模型。我正在尝试在 books 表中创建一个名为 author_id 的新列,它实际上是 users 表的外键。 我关注了this article by Josh
我遇到了 Minitest 和关联 ActiveRecord 模型 (Rails.4.2.3) 的问题。 这是两个模型: # vanguard_fund.rb class VanguardFund <
我是一名优秀的程序员,十分优秀!