Skip to content

Commit 4d5f91a

Browse files
committed
Add foreign keys to shipping method related models
The `Spree::ShippingMethodCategory` is a join model and needs foreign keys on both ends. The shipping method id on products was mandatory before, so it's safe to remove the `optional: true` on the `belongs_to` declaration and add a foreign key constraint here as well.
1 parent 3867232 commit 4d5f91a

3 files changed

Lines changed: 12 additions & 4 deletions

File tree

core/app/models/spree/product.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Product < Spree::Base
3131
has_many :taxons, through: :classifications, before_remove: :remove_taxon
3232

3333
belongs_to :tax_category, class_name: 'Spree::TaxCategory', optional: true
34-
belongs_to :shipping_category, class_name: 'Spree::ShippingCategory', inverse_of: :products, optional: true
34+
belongs_to :shipping_category, class_name: 'Spree::ShippingCategory', inverse_of: :products
3535
belongs_to :primary_taxon, class_name: 'Spree::Taxon', optional: true
3636

3737
has_one :master,
@@ -122,7 +122,6 @@ def find_or_build_master
122122
validates :meta_title, length: { maximum: 255 }
123123
validates :name, presence: true
124124
validates :price, presence: true, if: proc { Spree::Config[:require_master_price] }
125-
validates :shipping_category_id, presence: true
126125
validates :slug, presence: true, uniqueness: { allow_blank: true, case_sensitive: true }
127126

128127
attr_accessor :option_values_hash

core/app/models/spree/shipping_method_category.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module Spree
44
class ShippingMethodCategory < Spree::Base
5-
belongs_to :shipping_method, class_name: 'Spree::ShippingMethod', optional: true
6-
belongs_to :shipping_category, class_name: 'Spree::ShippingCategory', inverse_of: :shipping_method_categories, optional: true
5+
belongs_to :shipping_method, class_name: 'Spree::ShippingMethod'
6+
belongs_to :shipping_category, class_name: 'Spree::ShippingCategory', inverse_of: :shipping_method_categories
77
end
88
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
class AddShippingCategoryForeignKeys < ActiveRecord::Migration[7.0]
4+
def change
5+
add_foreign_key :spree_products, :spree_shipping_categories, column: :shipping_category_id, null: false
6+
add_foreign_key :spree_shipping_method_categories, :spree_shipping_methods, column: :shipping_method_id, null: false
7+
add_foreign_key :spree_shipping_method_categories, :spree_shipping_categories, column: :shipping_category_id, null: false
8+
end
9+
end

0 commit comments

Comments
 (0)