Skip to content

Commit b0f5448

Browse files
committed
Don't disable referential integrity for the same table twice
1 parent ed8b170 commit b0f5448

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def reset!
189189

190190
def tables_with_referential_integrity
191191
schemas_and_tables = select_rows <<-SQL.strip_heredoc
192-
SELECT s.name, o.name
192+
SELECT DISTINCT s.name, o.name
193193
FROM sys.foreign_keys i
194194
INNER JOIN sys.objects o ON i.parent_object_id = o.OBJECT_ID
195195
INNER JOIN sys.schemas s ON o.schema_id = s.schema_id

test/cases/adapter_test_sqlserver.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,11 @@ class AdapterTestSQLServer < ActiveRecord::TestCase
231231
end
232232
end
233233

234+
it 'not disable referential integrity for the same table twice' do
235+
tables = SSTestHasPk.connection.tables_with_referential_integrity
236+
assert_equal tables.size, tables.uniq.size
237+
end
238+
234239
end
235240

236241
describe 'database statements' do

test/schema/sqlserver_specific_schema.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,20 @@
144144

145145
# Constraints
146146

147-
create_table(:sst_has_fks, force: true) { |t| t.column(:fk_id, :bigint, null: false) }
147+
create_table(:sst_has_fks, force: true) do |t|
148+
t.column(:fk_id, :bigint, null: false)
149+
t.column(:fk_id2, :bigint)
150+
end
151+
148152
create_table(:sst_has_pks, force: true) { }
149153
execute <<-ADDFKSQL
150154
ALTER TABLE sst_has_fks
151155
ADD CONSTRAINT FK__sst_has_fks_id
152156
FOREIGN KEY ([fk_id])
157+
REFERENCES [sst_has_pks] ([id]),
158+
159+
CONSTRAINT FK__sst_has_fks_id2
160+
FOREIGN KEY ([fk_id2])
153161
REFERENCES [sst_has_pks] ([id])
154162
ADDFKSQL
155163

0 commit comments

Comments
 (0)