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

Cannot map nullable types to FlatTableProjections #3400

Open
nathanjcollins opened this issue Sep 5, 2024 · 2 comments
Open

Cannot map nullable types to FlatTableProjections #3400

nathanjcollins opened this issue Sep 5, 2024 · 2 comments
Labels
Milestone

Comments

@nathanjcollins
Copy link

When I use a FlatTableProjection, I cannot use non string nullables.

public record ImportFailedWithStringErrorCode(string? ErrorCodeString);

public record ImportFailedWithGuidErrorCode(Guid? ErrorCodeGuid);

public class FlatImportProjection : FlatTableProjection
{
    public FlatImportProjection()
        : base("import_history", SchemaNameSource.EventSchema)
    {
        Table.AddColumn<Guid>("id").AsPrimaryKey();

        TeardownDataOnRebuild = true;

        Project<ImportStarted>(map =>
        {
            map.Map(x => x.Started);
        });

        Project<ImportFailedWithStringErrorCode>(map =>
        {
            map.Map(x => x.ErrorCodeString);
        });

        Project<ImportFailedWithGuidErrorCode>(map =>
        {
            map.Map(x => x.ErrorCodeGuid);
        });
    }
}

If an event of ImportFailedWithGuidErrorCode is created with null, it will fail with the following error:

MessageText: function ms.mt_upsert_import_history_importfailedwithguiderrorcode(uuid, text) does not exist
Hint: No function matches the given name and argument types. You might need to add explicit type casts.

ImportFailedWithStringErrorCode works when passing null.

We have also tried other types, but it only seems to work with nullable strings.

There is a workaround using raw sql, but it's not ideal:

        ops.QueueSqlCommand(
            $"""
            insert into {TableName} (
                ErrorCodeGuid
            )
            values (
                ?::uuid /* note we have to explicitly cast it to a uuid here */
            )
            """,
            e.Data.ErrorCodeGuid?.ToString()!
        );
@jeremydmiller
Copy link
Member

jeremydmiller commented Sep 8, 2024

@nathanjcollins This is going to be too involved a change for the next quick release. Maybe later in the week, or feel free to dig into the flat table support to make the generated ADO.Net manipulation code robust enough to handle the nullable types. Or, use a default value for the Guid now.

There's an open branch (https://github.com/JasperFx/marten/tree/flat-table-projections) for reproducing it. I also hit a world of friction on the DateTimeOffset values, but I'm 100% blaming Npgsql for making date time types hard to use in their recent releases.

@nathanjcollins
Copy link
Author

@nathanjcollins This is going to be too involved a change for the next quick release. Maybe later in the week, or feel free to dig into the flat table support to make the generated ADO.Net manipulation code robust enough to handle the nullable types. Or, use a default value for the Guid now.

There's an open branch (https://github.com/JasperFx/marten/tree/flat-table-projections) for reproducing it. I also hit a world of friction on the DateTimeOffset values, but I'm 100% blaming Npgsql for making date time types hard to use in their recent releases.

Thanks for taking a look for us, hopefully we'll get some free time to take a look ourselves. But for now we will use the workaround with manual sql queries :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants