Skip to content

Commit

Permalink
Don't scaffold default index method annotation
Browse files Browse the repository at this point in the history
When scaffolding, we have an Npgsql-specific IndexMethod annotation to
represent PostgreSQL's index methods. Previously, we would always
output this annotation on indices, even when the method was the
default (btree); NpgsqlAnnotationCodeGenerator would elide it as by-
convention.

Although this is cleaner, it caused issues whenever two indices where
defined on the same column:
dotnet/efcore#11846

We now scaffold the annotation only for non-default index methods as a
workaround; the issue should now affect much less people.

Fixes #228
  • Loading branch information
roji committed May 26, 2018
1 parent c1abc10 commit 38d2176
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,14 @@ NOT indisprimary
if (predicate != null)
index.Filter = predicate;

index[NpgsqlAnnotationNames.IndexMethod] = record.GetValueOrDefault<string>("amname");
// It's cleaner to always output the index method on the database model,
// even when it's btree (the default);
// NpgsqlAnnotationCodeGenerator can then omit it as by-convention.
// However, because of https://github.com/aspnet/EntityFrameworkCore/issues/11846 we omit
// the annotation from the model entirely.
var indexMethod = record.GetValueOrDefault<string>("amname");
if (indexMethod != "btree")
index[NpgsqlAnnotationNames.IndexMethod] = indexMethod;

table.Indexes.Add(index);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1505,8 +1505,14 @@ public void Index_method()
var methodIndex = table.Indexes.Single(i => i.Name == "ix_a");
Assert.Equal("hash", methodIndex.FindAnnotation(NpgsqlAnnotationNames.IndexMethod).Value);
// It's cleaner to always output the index method on the database model,
// even when it's btree (the default);
// NpgsqlAnnotationCodeGenerator can then omit it as by-convention.
// However, because of https://github.com/aspnet/EntityFrameworkCore/issues/11846 we omit
// the annotation from the model entirely.
var noMethodIndex = table.Indexes.Single(i => i.Name == "ix_b");
Assert.Equal("btree", noMethodIndex.FindAnnotation(NpgsqlAnnotationNames.IndexMethod).Value);
Assert.Null(noMethodIndex.FindAnnotation(NpgsqlAnnotationNames.IndexMethod));
//Assert.Equal("btree", noMethodIndex.FindAnnotation(NpgsqlAnnotationNames.IndexMethod).Value);
},
@"DROP TABLE ""IndexMethod""");
}
Expand Down

0 comments on commit 38d2176

Please sign in to comment.