Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[Contrib][T4][SQL Server] Fix retrive index definition when table dei…

…nfinition has more index

When you have table with this schema
CREATE TABLE [Test Issue](
[ID_Primary] [int] IDENTITY(1,1) NOT NULL,
[Description] [nvarchar](50) NULL,
[ID_Nation] [int] NOT NULL,
CONSTRAINT [PK_Test Issue] PRIMARY KEY CLUSTERED
(
[ID_Primary] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY =
OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [Test Issue] ADD  CONSTRAINT [DF_Test Issue_ID_Nation]
DEFAULT ((0)) FOR [ID_Nation]
GO

the T4 template generated the following incorrect migration:

[Migration(20121120061957)]
public class CreateInitialDB : Migration
{
public override void Up()
{
//For Test Issue
Create.Table("Test Issue").InSchema("dbo")

.WithColumn("ID_Primary").AsInt32().PrimaryKey().Identity().NotNullable()
.WithColumn("Description").AsString().Nullable()
.WithColumn("ID_Nation").AsInt32().NotNullable();

Create.Index("IX_Nation").OnTable("Test Issue").InSchema("dbo")
.OnColumn("ID_Nation").Ascending()
.OnColumn("ID_Primary").Ascending();
}

public override void Down()
{

Delete.Index("IX_Nation");

Delete.Table("Test Issue");
}
}
  • Loading branch information...
commit b5bd4284a7e14d6289e188e7ace861c1a2bdddbe 1 parent 3af22b5
@spaccabit authored
Showing with 2 additions and 2 deletions.
  1. +2 −2 src/FluentMigrator.T4/SchemaReaders/SqlServerSchemaReader.cs
View
4 src/FluentMigrator.T4/SchemaReaders/SqlServerSchemaReader.cs
@@ -107,8 +107,8 @@ List<TableIndex> LoadIndices(Table tbl)
I.[is_unique_constraint], I.[fill_factor], I.[is_padded], I.[is_disabled], I.[is_hypothetical],
I.[allow_row_locks], I.[allow_page_locks], IC.[is_descending_key], IC.[is_included_column]
FROM sys.[tables] AS T
- INNER JOIN sys.[indexes] I ON T.[object_id] = I.[object_id]
- INNER JOIN sys.[index_columns] IC ON I.[object_id] = IC.[object_id]
+ INNER JOIN sys.[indexes] I ON T.[object_id] = I.[object_id]
+ INNER JOIN sys.[index_columns] IC ON I.[object_id] = IC.[object_id] AND I.index_id = IC.index_id
INNER JOIN sys.[all_columns] AC ON T.[object_id] = AC.[object_id] AND IC.[column_id] = AC.[column_id]
WHERE T.[is_ms_shipped] = 0 AND I.[type_desc] <> 'HEAP'
AND I.is_primary_key = 0 AND T.[name] = @tableName and OBJECT_SCHEMA_NAME(T.[object_id],DB_ID()) = @schemaName";
Please sign in to comment.
Something went wrong with that request. Please try again.