{"action":"created","issue":{"url":"https://api.github.com/repos/elixir-ecto/ecto/issues/1648","repository_url":"https://api.github.com/repos/elixir-ecto/ecto","labels_url":"https://api.github.com/repos/elixir-ecto/ecto/issues/1648/labels{/name}","comments_url":"https://api.github.com/repos/elixir-ecto/ecto/issues/1648/comments","events_url":"https://api.github.com/repos/elixir-ecto/ecto/issues/1648/events","html_url":"https://github.com/elixir-ecto/ecto/issues/1648","id":173131675,"number":1648,"title":"Fields that default to empty string have unexpected behavior","user":{"login":"bgeihsgt","id":707768,"avatar_url":"https://avatars1.githubusercontent.com/u/707768?v=4","gravatar_id":"","url":"https://api.github.com/users/bgeihsgt","html_url":"https://github.com/bgeihsgt","followers_url":"https://api.github.com/users/bgeihsgt/followers","following_url":"https://api.github.com/users/bgeihsgt/following{/other_user}","gists_url":"https://api.github.com/users/bgeihsgt/gists{/gist_id}","starred_url":"https://api.github.com/users/bgeihsgt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bgeihsgt/subscriptions","organizations_url":"https://api.github.com/users/bgeihsgt/orgs","repos_url":"https://api.github.com/users/bgeihsgt/repos","events_url":"https://api.github.com/users/bgeihsgt/events{/privacy}","received_events_url":"https://api.github.com/users/bgeihsgt/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":10,"created_at":"2016-08-25T07:26:34Z","updated_at":"2017-11-15T23:23:31Z","closed_at":"2016-08-28T22:52:11Z","author_association":"NONE","body":"### Environment\n- Elixir version (elixir -v): 1.3.2\n- Database and version (PostgreSQL 9.4, MongoDB 3.2, etc.): PostgreSQL 9.5\n- Ecto version (mix deps): 2.04\n- Database adapter and version (mix deps):\n- Operating system: OS X\n### Current behavior\n\nThe commit for `empty_values` (https://github.com/elixir-ecto/ecto/commit/af26b32854dce236cffc7e36ad0b9acad89969b7) has caused some unexpected behavior for fields that you want to default to empty string.\n\nI have a `notes` field defined like this:\n\n```\nfield :notes, :string, default: \"\"\n```\n\nWhen I create a changeset without a `notes` key, the column gets correctly set to `\"\"` in the DB.\n\nWhen I create a changeset with `notes` set to `\"\"`, the field gets sent to the DB as NULL. This breaks a not-null constract I have on that column.\n### Expected behavior\n\nA few things here:\n- I'm not sure converting to `nil` is the right answer here. I understand the reasoning to do this (HTML forms don't send nil/null), but is that really in Ecto's scope to handle? Empty string and `nil` are semantically different and to treat them the same by default feels like over-reach and coupling Ecto too tightly to the web (Ecto 2.0 was designed to be a move away from over-reach, right?). Perhaps `empty_values` should be opt-in instead of set to `[\"\"]` by default?\n- I've set a default on this field. If the default is set and I've set it to an empty value (`nil` or `\"\"` in this case), I'd expect it to use the default value.\n- `empty_values` as a setting on the entire changeset feels too coarse. I'd like to say \"Hey just for notes, don't use \"\" as an empty value\" instead of setting empty_values to `[]` for the entire changeset.\n\nThanks for this great library!\n"},"comment":{"url":"https://api.github.com/repos/elixir-ecto/ecto/issues/comments/344762899","html_url":"https://github.com/elixir-ecto/ecto/issues/1648#issuecomment-344762899","issue_url":"https://api.github.com/repos/elixir-ecto/ecto/issues/1648","id":344762899,"user":{"login":"atomkirk","id":2091439,"avatar_url":"https://avatars3.githubusercontent.com/u/2091439?v=4","gravatar_id":"","url":"https://api.github.com/users/atomkirk","html_url":"https://github.com/atomkirk","followers_url":"https://api.github.com/users/atomkirk/followers","following_url":"https://api.github.com/users/atomkirk/following{/other_user}","gists_url":"https://api.github.com/users/atomkirk/gists{/gist_id}","starred_url":"https://api.github.com/users/atomkirk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/atomkirk/subscriptions","organizations_url":"https://api.github.com/users/atomkirk/orgs","repos_url":"https://api.github.com/users/atomkirk/repos","events_url":"https://api.github.com/users/atomkirk/events{/privacy}","received_events_url":"https://api.github.com/users/atomkirk/received_events","type":"User","site_admin":false},"created_at":"2017-11-15T23:23:31Z","updated_at":"2017-11-15T23:23:31Z","author_association":"NONE","body":"Wait, @josevalim, how does that work?\r\n```\r\nchangeset = Ecto.Changeset.change(struct, %{})\r\nchangeset = %{changeset | empty_values: []}\r\nEcto.Changeset.cast(struct, params, ~w(name)a)\r\n```\r\nyou're not using the changeset you created in the cast.\r\nI need to use cast AND turn off the empty values"}}
{"actor":{"display_login":"atomkirk"}}