Skip to content

Commit

Permalink
Add foreign key constraint to Spree::OrderPromotions table
Browse files Browse the repository at this point in the history
This adds a foreign key constraint that makes sure at the database level
that we don't have non-existent order IDs in the spree_orders_promotions
table.
  • Loading branch information
mamhoff committed Oct 27, 2023
1 parent d8dcd14 commit 9e4a2c9
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions core/db/migrate/20231027084517_add_order_promotions_foreign_key.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class AddOrderPromotionsForeignKey < ActiveRecord::Migration[7.0]
def up
Spree::OrderPromotion.left_joins(:order).where(spree_orders: { id: nil }).delete_all
add_foreign_key :spree_orders_promotions, :spree_orders, column: :order_id, validate: false, on_delete: :cascade
end

def down
remove_foreign_key :spree_orders_promotions, :spree_orders
end
end

0 comments on commit 9e4a2c9

Please sign in to comment.