Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/active_record/connection_adapters/sqlserver_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def reset!

def tables_with_referential_integrity
schemas_and_tables = select_rows <<-SQL.strip_heredoc
SELECT s.name, o.name
SELECT DISTINCT s.name, o.name
FROM sys.foreign_keys i
INNER JOIN sys.objects o ON i.parent_object_id = o.OBJECT_ID
INNER JOIN sys.schemas s ON o.schema_id = s.schema_id
Expand Down
5 changes: 5 additions & 0 deletions test/cases/adapter_test_sqlserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,11 @@ class AdapterTestSQLServer < ActiveRecord::TestCase
end
end

it 'not disable referential integrity for the same table twice' do
tables = SSTestHasPk.connection.tables_with_referential_integrity
assert_equal tables.size, tables.uniq.size
end

end

describe 'database statements' do
Expand Down
10 changes: 9 additions & 1 deletion test/schema/sqlserver_specific_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,20 @@

# Constraints

create_table(:sst_has_fks, force: true) { |t| t.column(:fk_id, :bigint, null: false) }
create_table(:sst_has_fks, force: true) do |t|
t.column(:fk_id, :bigint, null: false)
t.column(:fk_id2, :bigint)
end

create_table(:sst_has_pks, force: true) { }
execute <<-ADDFKSQL
ALTER TABLE sst_has_fks
ADD CONSTRAINT FK__sst_has_fks_id
FOREIGN KEY ([fk_id])
REFERENCES [sst_has_pks] ([id]),

CONSTRAINT FK__sst_has_fks_id2
FOREIGN KEY ([fk_id2])
REFERENCES [sst_has_pks] ([id])
ADDFKSQL

Expand Down