Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using HasData to seed an Entity with a mapped Enum creates a migration that errors with: PostgresException (0x80004005) 42703: column "{{enum value}}" #3426

Open
JoshuaNitschke opened this issue Dec 31, 2024 · 0 comments

Comments

@JoshuaNitschke
Copy link

JoshuaNitschke commented Dec 31, 2024

Hi,

I think this is a minor bug on migration code generation, and while I found a work around, I thought I'd contribute my findings. I searched issues and didn't see anyone mention this yet.

Consider the below setup where Voice is an Entity model, and it has a reference to an enumeration, VoiceSource

An enumeration with Npgsql mapping added

public enum VoiceSource
{
    Recording = 0,
    Azure = 1,
}
opt.UseNpgsql(
        builder.Configuration.GetConnectionString("ZabaanDbContext"),
        o =>
        {
            o.MapEnum<VoiceSource>(nameof(VoiceSource));
            o.UseNodaTime();
        }
    );

I have some seed data configured:

public static readonly Voice Voices_AzureFarid = new Voice
{
    Id = VoiceId.AzureFarid, // vogen wrapped urn
    Source = VoiceSource.Azure,
    SourceIdentifier = "fa-IR-FaridNeural",
    Name = "Farid",
};

public static readonly Voice Voices_AzureDilara = new Voice
{
    Id = VoiceId.AzureDilara, // vogen wrapped urn
    Source = VoiceSource.Azure,
    SourceIdentifier = "fa-IR-DilaraNeural",
    Name = "Dilara",
};

modelBuilder.Entity<Voice>(voice =>
{
    voice.HasIndex(e => new { e.Source, SourceIdentifer = e.SourceIdentifier }).IsUnique();
    voice.HasIndex(e => e.Name).IsUnique();
    voice.HasData(Voices_AzureFarid, Voices_AzureDilara);
});

Then, when I make my INITIAL database migration, this code is generated where the enumeration is referenced directly:

migrationBuilder.InsertData(
    table: "Voices",
    columns: new[] { "Id", "Name", "Source", "SourceIdentifier" },
    values: new object[,]
    {
        { "urn:zabaan:voice:0a57108b-18d7-4877-ac09-67d73150da08", "Dilara", VoiceSource.Azure, "fa-IR-DilaraNeural" },
        { "urn:zabaan:voice:d0d96aa5-971b-4d78-a606-471469663c34", "Farid", VoiceSource.Azure, "fa-IR-FaridNeural" }
    });

Which fails to run via the migration bundle application:

INSERT INTO "Voices" ("Id", "Name", "Source", "SourceIdentifer")
VALUES ('urn:zabaan:voice:d0d96aa5-971b-4d78-a606-471469663c34', 'Farid', Azure, 'fa-IR-FaridNeural');
Npgsql.PostgresException (0x80004005): 42703: column "azure" does not exist

The fix

Updating the migration file manually to use the string value wrapped with single quotes fixed the issue and the migration succeeds:

migrationBuilder.InsertData(
    table: "Voices",
    columns: new[] { "Id", "Name", "Source", "SourceIdentifier" },
    values: new object[,]
    {
        { "urn:zabaan:voice:0a57108b-18d7-4877-ac09-67d73150da08", "Dilara", "\'azure\'", "fa-IR-DilaraNeural" },
        { "urn:zabaan:voice:d0d96aa5-971b-4d78-a606-471469663c34", "Farid", "\'azure\'" , "fa-IR-FaridNeural" }
    });

The single quotes wrapping the "azure" are necessary FYI, I first tried it without and that also failed.

Failed executing DbCommand (56ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
INSERT INTO "Voices" ("Id", "Name", "Source", "SourceIdentifer")
VALUES ('urn:zabaan:voice:0a57108b-18d7-4877-ac09-67d73150da04', 'Dilara', azure, 'fa-IR-DilaraNeural');
INSERT INTO "Voices" ("Id", "Name", "Source", "SourceIdentifer")
VALUES ('urn:zabaan:voice:d0d96aa5-971b-4d78-a606-471469663c38', 'Farid', azure, 'fa-IR-FaridNeural');
Npgsql.PostgresException (0x80004005): 42703: column "azure" does not exist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant