-
Notifications
You must be signed in to change notification settings - Fork 359
Saving a null
Postgres enum results in a varchar value
#1935
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
Comments
I think it isn't Spring Data JDBC that tries to convert something here. Instead it seems to tell Postgres, this |
Yeah I debugged into the JdbcTemplate and could see it was trying to insert with sqlType = 12 = varchar. Not sure at what point spring-data-jdbc is telling Postgres is is a varchar though |
@schauder Do you think this is a bug? It seems like the MappingJdbcConverter doesn't recognise the type so converts it to a String. Should there be some function further up the call stack that handles nulls whereby if the value is null then don't try and type it and insert as a null in the DB? |
Yes, I do think this is a bug. Could you provide a full reproducer, preferable as github repo? |
@schauder Thank you :) here is an app that reproduces it https://github.com/mattmcc-attest/animal |
^ is the above useful for you? @schauder |
Yes, I think this will do. Just need to find time to look into it. |
no problem, thanks for your help |
Hi, everyone!
The first test is correct because inside converters we have type information by which can find custom converter The second test is incorrect because we don't know what type information for value we have. But even if we do this, for example like this (
And added some new logic where we can find a specific converter, as a result of this convert null object -> null JdbcValue. Also in the method's documentation by Converter#convert method source must be not null. Hence, MappingJdbcConverter will choose default converter by target type (is a String) |
As a new approach, I can suggest that you set the default JDBC Type.NULL for null values. I tested this approach on https://github.com/mattmcc-attest/animal the repositories and animal were saved correctly. I can push changes and a few tests so that you can see this approach :) |
Signed-off-by: Sergey Korotaev <[email protected]> Original pull request #2068 Closes #1935
Not all databases support the JDBCSqlType.NULL. Therefore this handling was made dialect dependent, with SQL Server and DB2 using the old approach, while all others use JDBCSqlType.NULL In the process modified AbstractJdbcConfiguration to use JdbcDialect instead of Dialect. Original pull request #2068 See #1935 See #2031
null
Postgres enum results in a varchar value
Hi, I have an issue with enums using spring-data-jdbc.
I have a Java enum type:
And a corresponding enum type on one of my tables:
I have this Entity (see the Sentiment field):
If I call save on the repository:
I get the error:
I have some Converters for handling the enums but because the value is null it doesn't reach these converters. It seems to me that the spring-data-jdbc library must be at some point trying to convert the null value to a varchar? (maybe by default?)
This is not what I would expect, I would expect if the value I am inserting is null and my enum type postgres field is nullable then null should be used rather than varchar :)
Let me know if you need any more information, thank you!
The text was updated successfully, but these errors were encountered: