Ruby on rails Rails在表上更新或删除;产品“;违反外键约束

Ruby on rails Rails在表上更新或删除;产品“;违反外键约束,ruby-on-rails,migration,key,Ruby On Rails,Migration,Key,我正在Rails上开发一个应用程序来销售一些产品,其中包含一个表用户、一个表产品和一个表评论,用户可以在其中对给定的产品编写自己的评论;现在我开始实现支付系统,从monetize gem开始,但是在我将price字段添加到Product表并运行rails db:seed之后,会导致与Reviews表发生冲突。。。需要一些建议。当前代码如下所示: product.rb class Product < ApplicationRecord has_one_attached :photo

我正在Rails上开发一个应用程序来销售一些产品,其中包含一个表用户、一个表产品和一个表评论,用户可以在其中对给定的产品编写自己的评论;现在我开始实现支付系统,从monetize gem开始,但是在我将price字段添加到Product表并运行rails db:seed之后,会导致与Reviews表发生冲突。。。需要一些建议。当前代码如下所示:

product.rb

class Product < ApplicationRecord
  has_one_attached :photo

  has_many :reviews, dependent: :nullify

  monetize :price_cents

  include PgSearch
  pg_search_scope :search_by_full_name, against: [:name]
end
class Review < ApplicationRecord
  belongs_to :user
  belongs_to :product
end
ActiveRecord::Schema.define(version: 2020_11_11_190245) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "active_storage_attachments", force: :cascade do |t|
    t.string "name", null: false
    t.string "record_type", null: false
    t.bigint "record_id", null: false
    t.bigint "blob_id", null: false
    t.datetime "created_at", null: false
    t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id"
    t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true
  end

  create_table "active_storage_blobs", force: :cascade do |t|
    t.string "key", null: false
    t.string "filename", null: false
    t.string "content_type"
    t.text "metadata"
    t.bigint "byte_size", null: false
    t.string "checksum", null: false
    t.datetime "created_at", null: false
    t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
  end

  create_table "products", force: :cascade do |t|
    t.string "name"
    t.string "description"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.integer "price_cents", default: 0, null: false
  end

  create_table "reviews", force: :cascade do |t|
    t.text "description"
    t.bigint "user_id", null: false
    t.bigint "product_id", null: false
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["product_id"], name: "index_reviews_on_product_id"
    t.index ["user_id"], name: "index_reviews_on_user_id"
  end

  create_table "users", force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.string "first_name"
    t.string "last_name"
    t.string "phone_number"
    t.string "address"
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

  add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
  add_foreign_key "reviews", "products"
  add_foreign_key "reviews", "users"
end
有时错误是这样的 PG::NotNullViolation:错误:“产品id”列中的null值违反了非null约束


感谢您的帮助

您的种子文件看起来怎么样?我想问题就在那里。它看起来像是在创造一些东西,从您共享的代码中,我会猜测
评论
属于
产品
,但它没有给出
产品id
,这就是为什么您会出现错误的原因。查找其来源,然后确保包含相关
产品的
product\u id
值。如果我运行rails console,每次查看时都会给我查看的id、用户id和所指产品的id,所以那部分是working@felipeecst下面是我的种子文件的一个示例:product=product.create(名称:“Hidratante下士em Barra”,描述:“leo de côco bio;Manteiga de karitébio;leo de baobab;Cera candelila;OE(benjoim;salva esclareia;香根草;s–ndalo amyris;依兰)”)file=URI.open(“)product.photo.attach(io:file,文件名:'product.jpg',内容类型:'image/jpg')