【问题标题】:PG::ForeignKeyViolation: ERROR: insert or update on table violates foreign key constraintPG::ForeignKeyViolation:错误:插入或更新表违反外键约束
【发布时间】:2018-01-29 15:55:54
【问题描述】:

我已将我的 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

Product.rb

class Product < ApplicationRecord
  has_many :stocks, -> { group(:store_code) }, foreign_key: :sku, primary_key: :sku 
  has_many :stores, through: :stocks
end

Stock.rb

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 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

错误是说 sku 不在产品表中,但它在。如果我在 products 表上的那个 sku 上运行 SQL 查询,它会返回一行。

我做错了什么?

【问题讨论】:

  • 您从哪里获得sku16819?你怎么知道它是有效的?您确定要针对同一个数据库运行 SQL 查询吗?
  • 该 sku 来自我自己添加的产品。它在数据库中,因为我可以使用 Product.find_by(sku: 16819) 检索它
  • 您能否在 Product.find_by(sku: 16819) 工作的同一控制台中使用 SKU 创建一个 Stock
  • 没有。自从切换到 postgres 后,我无法以任何方式创建新 Stock。虽然在 SQLite 中工作
  • 从 products 表中返回一行。

标签: ruby-on-rails postgresql


【解决方案1】:

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

【讨论】:

  • 免责声明 db:migrate:reset 删除您的数据库,创建一个新数据库并运行迁移。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-16
  • 2020-02-07
  • 2013-09-06
  • 2011-01-27
  • 1970-01-01
  • 2020-12-02
  • 2021-12-12
相关资源
最近更新 更多