Skip to content

Commit 91d5c4d

Browse files
authored
Merge pull request #851 from BenMcH/master
Updated 'column_definitions_sql' to ensure that only primary key key constraints are queried for
2 parents 88a84da + 42c8e81 commit 91d5c4d

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

lib/active_record/connection_adapters/sqlserver/schema_statements.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ def column_definitions_sql(database, identifier)
472472
AND c.default_object_id = d.object_id
473473
LEFT OUTER JOIN #{database}.sys.key_constraints k
474474
ON c.object_id = k.parent_object_id
475+
AND k.type = 'PK'
475476
LEFT OUTER JOIN #{database}.sys.index_columns ic
476477
ON k.parent_object_id = ic.object_id
477478
AND k.unique_index_id = ic.index_id

test/cases/schema_dumper_test_sqlserver.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@ class SchemaDumperTestSQLServer < ActiveRecord::TestCase
135135
assert_line :name, type: "string", limit: nil, default: nil, collation: nil
136136
end
137137

138+
it "dumps field with unique key constraints only once" do
139+
output = generate_schema_for_table "unique_key_dumped_table"
140+
141+
_(output.scan('t.integer "unique_field"').length).must_equal(1)
142+
end
143+
138144
private
139145

140146
def generate_schema_for_table(*table_names)

test/schema/sqlserver_specific_schema.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,4 +277,14 @@
277277
field_2 int NOT NULL PRIMARY KEY,
278278
)
279279
SCHEMATESTMULTIPLESCHEMA
280+
281+
execute "IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'unique_key_dumped_table') DROP TABLE unique_key_dumped_table"
282+
execute <<-SQLSERVERUNIQUEKEYS
283+
CREATE TABLE unique_key_dumped_table (
284+
id int IDENTITY(1,1) NOT NULL,
285+
unique_field int DEFAULT 0 NOT NULL,
286+
CONSTRAINT IX_UNIQUE_KEY UNIQUE (unique_field),
287+
CONSTRAINT PK_UNIQUE_KEY PRIMARY KEY (id)
288+
);
289+
SQLSERVERUNIQUEKEYS
280290
end

0 commit comments

Comments
 (0)