Skip to content

Commit ec9705c

Browse files
committed
Filter table constraints with matching table schema to column. Fixes rails-sqlserver#478
1 parent 3d8741b commit ec9705c

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* Overwrite `_type_cast` method for char data. Fixes #491 #494
66
* Char type data compared against it's string value. Fixes #487 #488
7+
* Filter table constraints with matching table schema to column. Fixes #478
78

89

910
## v4.2.15

lib/active_record/connection_adapters/sqlserver/schema_statements.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ def column_definitions(table_name)
264264
FROM #{database}.INFORMATION_SCHEMA.COLUMNS columns
265265
LEFT OUTER JOIN #{database}.INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS TC
266266
ON TC.TABLE_NAME = columns.TABLE_NAME
267+
AND TC.TABLE_SCHEMA = columns.TABLE_SCHEMA
267268
AND TC.CONSTRAINT_TYPE = N'PRIMARY KEY'
268269
LEFT OUTER JOIN #{database}.INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KCU
269270
ON KCU.COLUMN_NAME = columns.COLUMN_NAME

test/cases/specific_schema_test_sqlserver.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,12 @@ def quoted_id
164164
db_uuid.must_match(acceptable_uuid)
165165
end
166166

167+
# with similar table definition in two schemas
168+
169+
it 'returns the correct primary columns' do
170+
connection = ActiveRecord::Base.connection
171+
assert_equal 'field_1', connection.columns('test.sst_schema_test_mulitple_schema').detect(&:is_primary?).name
172+
assert_equal 'field_2', connection.columns('test2.sst_schema_test_mulitple_schema').detect(&:is_primary?).name
173+
end
174+
167175
end

test/schema/sqlserver_specific_schema.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,20 @@
204204
)
205205
NATURALPKTABLESQLINOTHERSCHEMA
206206

207+
execute "IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'sst_schema_test_mulitple_schema' and TABLE_SCHEMA = 'test') DROP TABLE test.sst_schema_test_mulitple_schema"
208+
execute <<-SCHEMATESTMULTIPLESCHEMA
209+
CREATE TABLE test.sst_schema_test_mulitple_schema(
210+
field_1 int NOT NULL PRIMARY KEY,
211+
field_2 int,
212+
)
213+
SCHEMATESTMULTIPLESCHEMA
214+
execute "IF NOT EXISTS(SELECT * FROM sys.schemas WHERE name = 'test2') EXEC sp_executesql N'CREATE SCHEMA test2'"
215+
execute "IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'sst_schema_test_mulitple_schema' and TABLE_SCHEMA = 'test2') DROP TABLE test2.sst_schema_test_mulitple_schema"
216+
execute <<-SCHEMATESTMULTIPLESCHEMA
217+
CREATE TABLE test2.sst_schema_test_mulitple_schema(
218+
field_1 int,
219+
field_2 int NOT NULL PRIMARY KEY,
220+
)
221+
SCHEMATESTMULTIPLESCHEMA
222+
207223
end

0 commit comments

Comments
 (0)