|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'helper' |
| 4 | + |
| 5 | +# Composite foreign keys were introduced in Rails 7.2 |
| 6 | +if ActiveRecord::VERSION::MAJOR == 7 && ActiveRecord::VERSION::MINOR >= 2 || ActiveRecord::VERSION::MAJOR > 7 |
| 7 | + class CompositeForeignKeyParent < ActiveRecord::Base |
| 8 | + self.table_name = 'composite_foreign_key_parents' |
| 9 | + end |
| 10 | + |
| 11 | + class CompositeForeignKeyChild < ActiveRecord::Base |
| 12 | + self.table_name = 'composite_foreign_key_children' |
| 13 | + |
| 14 | + # Composite foreign key relationship |
| 15 | + belongs_to :composite_foreign_key_parent, |
| 16 | + foreign_key: [:parent_id, :child_key], |
| 17 | + primary_key: [:id, :name], |
| 18 | + optional: false |
| 19 | + |
| 20 | + acts_as_list scope: [:parent_id, :child_key, :category] |
| 21 | + end |
| 22 | + |
| 23 | + class CompositeForeignKeyTestCase < Minitest::Test |
| 24 | + def setup |
| 25 | + ActiveRecord::Base.connection.create_table :composite_foreign_key_parents do |t| |
| 26 | + t.string :name |
| 27 | + t.timestamps |
| 28 | + end |
| 29 | + |
| 30 | + ActiveRecord::Base.connection.create_table :composite_foreign_key_children do |t| |
| 31 | + t.integer :parent_id |
| 32 | + t.string :child_key |
| 33 | + t.string :category |
| 34 | + t.integer :position |
| 35 | + t.timestamps |
| 36 | + end |
| 37 | + |
| 38 | + ActiveRecord::Base.connection.schema_cache.clear! |
| 39 | + [CompositeForeignKeyParent, CompositeForeignKeyChild].each(&:reset_column_information) |
| 40 | + |
| 41 | + # Set up the has_many association dynamically |
| 42 | + CompositeForeignKeyParent.has_many :composite_foreign_key_children, |
| 43 | + foreign_key: [:parent_id, :child_key], |
| 44 | + primary_key: [:id, :name], |
| 45 | + dependent: :destroy |
| 46 | + end |
| 47 | + |
| 48 | + def teardown |
| 49 | + teardown_db |
| 50 | + end |
| 51 | + |
| 52 | + def test_allows_destroying_parent_with_composite_foreign_key_children |
| 53 | + parent = CompositeForeignKeyParent.create!(name: 'test') |
| 54 | + child1 = CompositeForeignKeyChild.create!( |
| 55 | + parent_id: parent.id, |
| 56 | + child_key: 'test', |
| 57 | + category: 'A', |
| 58 | + position: 1 |
| 59 | + ) |
| 60 | + child2 = CompositeForeignKeyChild.create!( |
| 61 | + parent_id: parent.id, |
| 62 | + child_key: 'test', |
| 63 | + category: 'A', |
| 64 | + position: 2 |
| 65 | + ) |
| 66 | + |
| 67 | + # When parent is destroyed with dependent: :destroy, it will destroy children |
| 68 | + # The child's destroyed_via_scope? method will be called |
| 69 | + # Without the fix, this raises: NoMethodError: undefined method 'to_sym' for an instance of Array |
| 70 | + assert parent.destroy |
| 71 | + assert_equal 0, CompositeForeignKeyChild.count |
| 72 | + end |
| 73 | + |
| 74 | + def test_allows_destroying_child_with_composite_foreign_key |
| 75 | + parent = CompositeForeignKeyParent.create!(name: 'test') |
| 76 | + child = CompositeForeignKeyChild.create!( |
| 77 | + parent_id: parent.id, |
| 78 | + child_key: 'test', |
| 79 | + category: 'A', |
| 80 | + position: 1 |
| 81 | + ) |
| 82 | + |
| 83 | + assert child.destroy |
| 84 | + end |
| 85 | + end |
| 86 | +end |
0 commit comments