sqlite - SQLite3::ConstraintException:唯一约束失败:action_text_rich_texts.record_type,action_text_rich_texts.record_id
问题描述
我无法创建新产品,但出现此错误product.valid? = true
:
ActiveRecord::RecordNotUnique in Admin::ProductsController#create
SQLite3::ConstraintException: UNIQUE constraint failed: action_text_rich_texts.record_type, action_text_rich_texts.record_id, action_text_rich_texts.name
我不知道为什么。这是我的产品架构:
create_table "products", force: :cascade do |t|
t.string "code"
t.integer "real_price"
t.integer "sale_price"
t.integer "remaining_amount"
t.string "title"
t.text "description"
t.text "detail"
t.string "seo_title"
t.string "seo_keyword"
t.string "seo_description"
t.integer "product_category_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["code"], name: "index_products_on_code", unique: true
t.index ["product_category_id"], name: "index_products_on_product_category_id"
t.index ["title"], name: "index_products_on_title", unique: true
end
这是我的产品模型:
class Product < ApplicationRecord
belongs_to :product_category
has_many_attached :images
has_rich_text :detail
validates :code, :title, presence: true
validates :code, :title, uniqueness: { case_sensitive: false }
end
这是我的管理员/products_controller.rb
class Admin::ProductsController < AdminController
...
def create
@product = Product.new(product_params)
if @product.save
redirect_to
else
render 'new'
end
end
private
...
def product_params
params.require(:product).permit(:code, :real_price, :sale_price, :remaining_amount,
:title, :description, :detail, :seo_title, :seo_keyword, :seo_description,
:product_category_id, images: [])
end
end
请帮我解决这个错误!
解决方案
推荐阅读
- javascript - 社区大学课程的 SIMPLE JS Twitter 代码
- reactjs - 如何使用更新的 api url 从同一组件中调用 getServerSideProps?
- .net - Blazor 和 AlpineJS——点击远离子组件
- javascript - 如何识别跨度并添加到它的类列表中
- javascript - 如何禁用/启用密钥
- javascript - Redux:无法读取未定义的属性“getState”
- python - 从 PNG 中提取数字信息
- google-kubernetes-engine - Liveness Probe,Readiness Probe 未在预期持续时间内调用
- sql - JSON扁平化和表创建
- github - GitBucket API如何获取带有基本名称的拉取请求