Updating a database field to NULL using Ecto

Is it possible to update a database field to NULL using Ecto.Repo? Basically, is it possible to do the following:

changeset
|> put_change(:middle_name, nil)
|> Repo.update()

Right now, the change to :middle_name is ignored because it is set to nil and Repo.update assumes there is no change to send to the database. However, I don’t want it to be ignored. I’ve read through the documentation, browsed this forum, and have done Google searches, but nothing pops up. Am I missing something here?

The best I could find is this post which makes reference to an allow_nil option on the schema. However, that option isn’t listed in the documentation and I couldn’t find it in the Github repo either.

I feel like I’m missing something here.

the code you posted should just work. are you maybe not reloading afterwards or forgot to assign the result?

It will also not work if you already have nil. I think there was a post already on the forum where person was asking the same question, it turned out that she/he had nil value already, and looked at resulting SQL and it generated nothing.

In short: ecto will only update the fields that do change. So if your row in databse has nil already, it will do nothing.