Null vs "" for Rails with Postgres, with JS client app

We just ran into a really thorny issue of null vs. “” when sending JSON to the client. Basically, putting a null in the JSON vs a "" might have very different results! as in CRASH!

Here’s a detailed write-up I grabbed from the Rogues Parley forum:

(Captured here if you’re not a subscriber)

  1. Here’s my take — the UI has no concept of displaying nil vs blank, so we should never send nil to the UI in the JSON. Rails will always send back a blank string.
  2. On the DB side, we could maintain consistency with one of several Rails gems that either force “” or NULL in the DB.
  3. We could also enforce consistency of no nulls at the DB level by specifying the options of null: false, default: "" in the DB.

This is about as annoying as ‘\’ versus ‘/‘ and I think Oracle had it right in this case.

What’s our best recommendation?

It’s worth considering that this issue really doesn’t matter for standard form based rails apps. The UI simply returns empty strings when posting, and it gets blanks when display.

1 Like

My take on this:

  • We should never use nullable on string or boolean columns. Avoid it by null: false, default: ""
  • For number or datetime columns could be nullable because in some scenario nil represent the missing value/input.