0

I have a REST API written in Spring that inserts data into a PostgreSQL database. I would like to avoid empty strings in the database -- and let empty fields be null instead.

Is there anything meaningful I can do in the database side?

For instance, I tried this and while the syntax works from the console, it won't work with named parameters when used by JdbcTemplate in Spring.

insert
...
nullif(trim(:value), '')

Or perhaps I should just clean the data programmatically?


Edit: To clarify, I am asking about the concepts, not about the specific way of achieving the goal provided as an example, or the resulting error message.

9
  • 1
    "it won't work with named parameters when used by JdbcTemplate in Spring" -- You should edit he question and provide a minimal reproducible example of such an issue and the exact error message. Commented Feb 15, 2021 at 13:39
  • The minimal reproducible example is this: 'nullif(trim(:value), '')'. ':value' is the named parameter. Commented Feb 15, 2021 at 13:48
  • That is not a minimal reproducible example! Commented Feb 15, 2021 at 13:51
  • That's an example of what I'm asking about. Of course I can wrap the example in entire application, but that's not the code I'm asking about. In fact, I'm not asking about any existing code at all, but about how this can / should be done. Commented Feb 15, 2021 at 13:53
  • A minimal reproducible example is required and it's not a minimal reproducible example. Read the definition behind the link. And the error message is also still missing. Commented Feb 15, 2021 at 13:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.