Skip to content

Commit 5a63bcd

Browse files
committed
Classifications: Add foreign keys
This join table needs both references to be pointing to valid records. A uniqueness index for the taxon id in this table is already present.
1 parent a5cded3 commit 5a63bcd

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

core/app/models/spree/classification.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ module Spree
44
class Classification < Spree::Base
55
self.table_name = 'spree_products_taxons'
66
acts_as_list scope: :taxon
7-
belongs_to :product, class_name: "Spree::Product", inverse_of: :classifications, touch: true, optional: true
8-
belongs_to :taxon, class_name: "Spree::Taxon", inverse_of: :classifications, touch: true, optional: true
7+
belongs_to :product, class_name: "Spree::Product", inverse_of: :classifications, touch: true
8+
belongs_to :taxon, class_name: "Spree::Taxon", inverse_of: :classifications, touch: true
99

1010
# For https://github.com/spree/spree/issues/3494
1111
validates :taxon_id, uniqueness: { scope: :product_id, message: :already_linked }
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
3+
class AddFkToClassifications < ActiveRecord::Migration[7.0]
4+
def up
5+
Spree::Classification.left_joins(:product).where(spree_products: { id: nil }).delete_all
6+
Spree::Classification.left_joins(:taxon).where(spree_taxons: { id: nil }).delete_all
7+
8+
add_foreign_key :spree_products_taxons, :spree_products, column: :product_id
9+
add_foreign_key :spree_products_taxons, :spree_taxons, column: :taxon_id
10+
end
11+
12+
def down
13+
remove_foreign_key :spree_products_taxons, :spree_products, column: :product_id
14+
remove_foreign_key :spree_products_taxons, :spree_taxons, column: :taxon_id
15+
end
16+
end

0 commit comments

Comments
 (0)