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

Can't map null value to non-nullable property (which is nullable) #29

Open
goncalo-oliveira opened this issue Jun 4, 2024 · 4 comments

Comments

@goncalo-oliveira
Copy link

goncalo-oliveira commented Jun 4, 2024

.NET 8
Norm.net 5.4.0
PostgreSQL 15.1 (Ubuntu 15.1-1.pgdg20.04+1) on aarch64-unknown-linux-gnu

Hi,

I'm seeing the following mapping error

Can't map null value for database field "claims" to non-nullable property "Claims".

Except that the Claims property is actually nullable, so the mapping should have worked I think.

The claims field in the database is an array and it is NULL at this point, yes.

claims text[] null,

In the same way, the class property is a nullable array

public string[]? Claims { get; set; }

Am I missing something?

@vbilopav
Copy link
Contributor

vbilopav commented Jun 5, 2024

Hi,

there are some limitations to the mapping of the nullable arrays, described here: https://vb-consulting.github.io/norm.net/docs/reference/read/#arrays-and-enums

I believe you can mitigate those limitations by using DbReader callback:
https://vb-consulting.github.io/norm.net/docs/reference/read/#dbreader-callback

Also, reference here: https://vb-consulting.github.io/norm.net/docs/reference/methods/#withreadercallback

But unfortunately, the DbReader callback isn't that elegant and straightforward.

@goncalo-oliveira
Copy link
Author

Hi @vbilopav,

Thanks for the quick response. Seems like the quick workaround is indeed to use the DbReader callback, as you suggested.

            var account = await connection.WithReaderCallback( p =>
            {
                // resolve as null
                if ( p.Reader.IsDBNull( p.Ordinal ) )
                {
                    return DBNull.Value;
                }

                return null;
            } )
            .ReadAsync<Account>( sql, parameters )
            .FirstOrDefaultAsync();

Although it seems like this is something that could easily be supported, since all I'm doing in the callback is to check if the value is IsDBNull; there's no data type checks or whatsoever. This doesn't mean that the actual implementation is that easy and I might not be seeing the big picture.

For the time being, I don't mind using this workaround, since I'll probably need it for jsonb types as well.

@vbilopav
Copy link
Contributor

vbilopav commented Jun 5, 2024

It's not easily supported. I've tried, and it's extremely difficult, given the current approach. It would have required me to rewrite everything from scratch.

@goncalo-oliveira
Copy link
Author

Not worth the trouble then, I'd say.

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

2 participants