Ruby on rails PG::ForeignKeyViolation:错误:表上的插入或更新违反外键约束

Ruby on rails PG::ForeignKeyViolation:错误:表上的插入或更新违反外键约束,ruby-on-rails,postgresql,Ruby On Rails,Postgresql,我已将我的rails应用程序从SQLite更新为postgres,当我尝试添加新产品库存时,出现以下错误: ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR: insert or update on table "stocks" violates foreign key constraint "fk_rails_6d8dd2a287" DETAIL: Key (sku)=(16819) is not present

我已将我的rails应用程序从SQLite更新为postgres,当我尝试添加新产品库存时,出现以下错误:

ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR:  insert or update on table "stocks" violates foreign key constraint "fk_rails_6d8dd2a287"
DETAIL:  Key (sku)=(16819) is not present in table "products".
: INSERT INTO "stocks" ("store_code", "sku", "quantity", "date", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"
Store.rb

class Store < ApplicationRecord
  has_many :stocks, -> { group(:sku) }, foreign_key: :store_code, primary_key: :store_code
  has_many :products, through: :stocks
end
class Product < ApplicationRecord
  has_many :stocks, -> { group(:store_code) }, foreign_key: :sku, primary_key: :sku 
  has_many :stores, through: :stocks
end
class Stock < ApplicationRecord
  belongs_to :store, primary_key: :store_code, foreign_key: :store_code, class_name: 'Store'
  belongs_to :product, primary_key: :sku, foreign_key: :sku, class_name: 'Product'
end
类存储{group(:sku)}、外键::存储码、主键::存储码
有很多:产品,通过股票
终止
Product.rb

class Store < ApplicationRecord
  has_many :stocks, -> { group(:sku) }, foreign_key: :store_code, primary_key: :store_code
  has_many :products, through: :stocks
end
class Product < ApplicationRecord
  has_many :stocks, -> { group(:store_code) }, foreign_key: :sku, primary_key: :sku 
  has_many :stores, through: :stocks
end
class Stock < ApplicationRecord
  belongs_to :store, primary_key: :store_code, foreign_key: :store_code, class_name: 'Store'
  belongs_to :product, primary_key: :sku, foreign_key: :sku, class_name: 'Product'
end
类产品{group(:store_code)}、外键::sku、主键::sku
有很多:商店,通过::库存
终止
股票.rb

class Store < ApplicationRecord
  has_many :stocks, -> { group(:sku) }, foreign_key: :store_code, primary_key: :store_code
  has_many :products, through: :stocks
end
class Product < ApplicationRecord
  has_many :stocks, -> { group(:store_code) }, foreign_key: :sku, primary_key: :sku 
  has_many :stores, through: :stocks
end
class Stock < ApplicationRecord
  belongs_to :store, primary_key: :store_code, foreign_key: :store_code, class_name: 'Store'
  belongs_to :product, primary_key: :sku, foreign_key: :sku, class_name: 'Product'
end
class Stock
我的迁移是:

class CreateStores < ActiveRecord::Migration[5.0]
  def change
    create_table :stores do |t|
      t.integer :store_code, null: false
      t.string :name
      t.string :address

      t.timestamps
    end
    add_index :stores, :store_code, unique: true
  end
end

class CreateProducts < ActiveRecord::Migration[5.0]
  def change
    create_table :products do |t|
      t.integer :sku, null: false
      t.string :product_type
      t.string :name
      t.decimal :price, precision: 15, scale: 2
      t.integer :volume
      t.decimal :abv, precision: 3, scale: 1
      t.integer :sweetness
      t.string :style
      t.string :country
      t.string :region
      t.integer :year
      t.text :notes

      t.timestamps
    end
    add_index :products, :sku, unique: true
  end
end

class CreateStocks < ActiveRecord::Migration[5.0]
  def change
    create_table :stocks do |t|
      t.integer :store_code, index: true
      t.integer :sku, index: true
      t.integer :quantity
      t.date :date, index: true

      t.timestamps
    end
    add_foreign_key :stocks, :stores, column: :store_code
    add_foreign_key :stocks, :products, column: :sku
  end
end
class CreateStores
错误是sku不在products表中,但在products表中。如果我对products表上的sku运行SQL查询,它将返回一行


我出了什么问题?

因此,通过重新启动并重置所有内容,问题得到了解决。我重新启动了postgres,退出了服务器和控制台,运行了rake db:migrate:reset
,现在我可以插入没有问题的股票了。

因此,通过重新启动和重置所有内容,问题得到了解决。我重新启动了postgres,退出了服务器和控制台,运行了
rake db:migrate:reset
,现在我可以毫无问题地插入股票了。

您从哪里获得
16819
sku
?您具体如何知道它是有效的?您确定要对同一个数据库运行SQL查询吗?该sku来自我自己添加的产品。它在数据库中,因为我可以用
产品检索它。find_by(sku:16819)
你能从
产品所在的同一控制台使用sku创建
股票吗。find_by(sku:16819)
有效吗?不。自从切换到postgres后,我无法以任何方式创建新股票。在SQLite中工作,但它从products表返回一行。您从哪里获得
16819
sku
?您具体如何知道它是有效的?您确定要对同一个数据库运行SQL查询吗?该sku来自我自己添加的产品。它在数据库中,因为我可以用
产品检索它。find_by(sku:16819)
你能从
产品所在的同一控制台使用sku创建
股票吗。find_by(sku:16819)
有效吗?不。自从切换到postgres后,我无法以任何方式创建新股票。在SQLite中工作,但它从products表返回一行。免责声明执行此操作将删除数据库,创建新数据库并运行迁移。免责声明执行此操作将删除数据库,创建新数据库并运行迁移。免责声明执行此操作将删除数据库,并运行迁移。