首页 > 解决方案 > 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

请帮我解决这个错误!

标签: sqliteruby-on-rails-6actiontext

解决方案


推荐阅读