Skip to content

Issues with configuring tsvector columns for migrations #3246

Closed as not planned

Description

@gabr0236

I want to add a tsvector column to my table for FTS. Im following Method 1 from the docs: https://www.npgsql.org/efcore/mapping/full-text-search.html?tabs=pg12%2Cv5

My model:

public class PostEntity {
    public Guid Id { get; init; }
    ...
    public string Content { get; set; }
    public NpgsqlTsVector SearchVector { get; set; }
}

My configuration:

public class SomeConfiguration : IEntityTypeConfiguration<PostEntity>
{
    public void Configure(EntityTypeBuilder<PostEntity> builder)
    {
        builder.HasGeneratedTsVectorColumn(
                p => p.SearchVector,
                "english", 
                p => p.Content)
            .HasIndex(p => p.SearchVector)
            .HasMethod("GIN");

The issue

When i run the migration i get this error:

Unhandled exception. System.InvalidOperationException: Column or index SearchVector refers to unknown column in tsvector definition
   at Npgsql.EntityFrameworkCore.PostgreSQL.Migrations.NpgsqlMigrationsSqlGenerator.ColumnsToTsVector(String columnOrIndexName, IEnumerable`1 columnNames, String tsVectorConfig, IModel model, String schema, String table)
   at Npgsql.EntityFrameworkCore.PostgreSQL.Migrations.NpgsqlMigrationsSqlGenerator.ApplyTsVectorColumnSql(ColumnOperation column, IModel model, String name, String schema, String table)
   at Npgsql.EntityFrameworkCore.PostgreSQL.Migrations.NpgsqlMigrationsSqlGenerator.ColumnDefinition(String schema, String table, String name, ColumnOperation operation, IModel model, MigrationCommandListBuilder builder)
   at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.ColumnDefinition(AddColumnOperation operation, IModel model, MigrationCommandListBuilder builder)
   at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.Generate(AddColumnOperation operation, IModel model, MigrationCommandListBuilder builder, Boolean terminate)
   at Npgsql.EntityFrameworkCore.PostgreSQL.Migrations.NpgsqlMigrationsSqlGenerator.Generate(AddColumnOperation operation, IModel model, MigrationCommandListBuilder builder, Boolean terminate)
   at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.<>c.<.cctor>b__82_0(MigrationsSqlGenerator g, MigrationOperation o, IModel m, MigrationCommandListBuilder b)
   at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.Generate(MigrationOperation operation, IModel model, MigrationCommandListBuilder builder)
   at Npgsql.EntityFrameworkCore.PostgreSQL.Migrations.NpgsqlMigrationsSqlGenerator.Generate(MigrationOperation operation, IModel model, MigrationCommandListBuilder builder)
   at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.Generate(IReadOnlyList`1 operations, IModel model, MigrationsSqlGenerationOptions options)
   at Npgsql.EntityFrameworkCore.PostgreSQL.Migrations.NpgsqlMigrationsSqlGenerator.Generate(IReadOnlyList`1 operations, IModel model, MigrationsSqlGenerationOptions options)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.GenerateUpSql(Migration migration, MigrationsSqlGenerationOptions options)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.<>c__DisplayClass16_2.<GetMigrationCommandLists>b__2()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.MigrateAsync(String targetMigration, CancellationToken cancellationToken)
   at Npgsql.EntityFrameworkCore.PostgreSQL.Migrations.Internal.NpgsqlMigrator.MigrateAsync(String targetMigration, CancellationToken cancellationToken)

It looks to me like the this line

var columns = columnNames
            .Select(columnName => model?.GetRelationalModel().FindTable(table, schema)?.Columns.FirstOrDefault(c => c.Name == columnName))
            .ToArray();

is somehow resulting in var columns = [null] 🤔

Further technical details

Npgsql version: 8.0.4
PostgreSQL version: 15.8
Operating system: M1 mac, Sonoma, 14.0

Activity

  1. transferred this issue fromnpgsql/npgsqlon Aug 19, 2024
  2. roji commented on Aug 19, 2024

    @roji
    Member

    Can you please submit a minimal console program that shows this happening?

  3. gabr0236 commented on Aug 19, 2024

    @gabr0236
    Author

    @roji i got it working on a new project 🤔 not quite sure what im doing wrong on the company project. I will do some more investigating and hopefully write here when i figure it out...

  4. roji commented on Aug 19, 2024

    @roji
    Member

    OK. I'll go ahead and close this for now as there's nothing immediately actionable, but if you discover something please post back here and I'll reopen.

  5. jufa2401 commented on Aug 19, 2024

    @jufa2401

    @gabr0236 and I found a workaround for our company project that works by splitting the migration in two steps:

    1. Creating a migration that simply adds a tsvector column and does not add any configuration of which columns to target
            protected override void Up(MigrationBuilder migrationBuilder)
            {
                migrationBuilder.AddColumn<NpgsqlTsVector>(
                    name: "PostContentSearchVector",
                    table: "Posts",
                    type: "tsvector",
                    nullable: true);
            }
    1. Creating a migration that alters the aforementioned column adding the TsVectorConfig with a value of english and TsVectorProperties targeting our properties.
            protected override void Up(MigrationBuilder migrationBuilder)
            {
                migrationBuilder.AlterColumn<NpgsqlTsVector>(
                    name: "PostContentSearchVector",
                    table: "Posts",
                    type: "tsvector",
                    nullable: true,
                    oldClrType: typeof(NpgsqlTsVector),
                    oldType: "tsvector",
                    oldNullable: true)
                    .Annotation("Npgsql:TsVectorConfig", "english")
                    .Annotation("Npgsql:TsVectorProperties", new[] { "Title", "Description" });
    
                migrationBuilder.CreateIndex(
                    name: "IX_StorylinesFeedPosts_PostContentSearchVector",
                    table: "StorylinesFeedPosts",
                    column: "PostContentSearchVector")
                    .Annotation("Npgsql:IndexMethod", "GIN");
            }

    Could it be that AddColumn<NpgsqlTsVector> and AlterColumn<NpgsqlTsVector> have different behaviours?

  6. roji commented on Aug 19, 2024

    @roji
    Member

    @jufa2401 it would still be good to see if/where all of this actually impacts your program (e.g. causes an error)...

  7. jufa2401 commented on Aug 19, 2024

    @jufa2401

    @jufa2401 it would still be good to see if/where all of this actually impacts your program (e.g. causes an error)...

    Ah sorry - it happens when executing dotnet ef database update

  8. jufa2401 commented on Aug 20, 2024

    @jufa2401

    After investigating further, it seems our issue happened only when deleting .Designer files of our migrations. When we retained our designer files it seems like everything works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions