aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/migration/foreign_key_test.rb
blob: 978d1a8cf19a96b429f285eaf47e67b6c064a05e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
require 'cases/helper'

if ActiveRecord::Base.connection.supports_foreign_keys?
module ActiveRecord
  class Migration
    class ForeignKeyTest < ActiveRecord::TestCase
      class Rocket < ActiveRecord::Base
      end

      class Astronaut < ActiveRecord::Base
      end

      setup do
        @connection = ActiveRecord::Base.connection
        @connection.create_table "rockets" do |t|
          t.string :name
        end

        @connection.create_table "astronauts" do |t|
          t.string :name
          t.references :rocket
        end
      end

      def test_add_foreign_key
        @connection.add_foreign_key :astronauts, :rockets, column: "rocket_id"

        assert_raises ActiveRecord::InvalidForeignKey do
          Astronaut.create rocket_id: 33
        end
      end

      def test_remove_foreign_key
        @connection.add_foreign_key :astronauts, :rockets, column: "rocket_id"
        @connection.remove_foreign_key :astronauts, column: "rocket_id"

        Astronaut.create rocket_id: 33
      end

      def test_remove_foreign_key_by_name
        @connection.add_foreign_key :astronauts, :rockets, column: "rocket_id", name: "fancy_named_fk"
        @connection.remove_foreign_key :astronauts, name: "fancy_named_fk"

        Astronaut.create rocket_id: 33
      end
    end
  end
end
end