{"action":"created","issue":{"url":"https://api.github.com/repos/elixir-lang/ecto/issues/591","labels_url":"https://api.github.com/repos/elixir-lang/ecto/issues/591/labels{/name}","comments_url":"https://api.github.com/repos/elixir-lang/ecto/issues/591/comments","events_url":"https://api.github.com/repos/elixir-lang/ecto/issues/591/events","html_url":"https://github.com/elixir-lang/ecto/pull/591","id":74763604,"number":591,"title":"Support database defaults during insert.","user":{"login":"joshwlewis","id":917672,"avatar_url":"https://avatars.githubusercontent.com/u/917672?v=3","gravatar_id":"","url":"https://api.github.com/users/joshwlewis","html_url":"https://github.com/joshwlewis","followers_url":"https://api.github.com/users/joshwlewis/followers","following_url":"https://api.github.com/users/joshwlewis/following{/other_user}","gists_url":"https://api.github.com/users/joshwlewis/gists{/gist_id}","starred_url":"https://api.github.com/users/joshwlewis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/joshwlewis/subscriptions","organizations_url":"https://api.github.com/users/joshwlewis/orgs","repos_url":"https://api.github.com/users/joshwlewis/repos","events_url":"https://api.github.com/users/joshwlewis/events{/privacy}","received_events_url":"https://api.github.com/users/joshwlewis/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-05-09T21:32:45Z","updated_at":"2015-05-10T06:38:35Z","closed_at":"2015-05-10T06:38:35Z","pull_request":{"url":"https://api.github.com/repos/elixir-lang/ecto/pulls/591","html_url":"https://github.com/elixir-lang/ecto/pull/591","diff_url":"https://github.com/elixir-lang/ecto/pull/591.diff","patch_url":"https://github.com/elixir-lang/ecto/pull/591.patch"},"body":"For a bit of background, consider this model:\r\n\r\n```elixir\r\ncreate table(:clients, primary_key: false) do\r\n  add :id, :uuid, primary_key: true, default: fragment(\"uuid_generate_v4()\")\r\n  add :name, :string, null: false\r\n  add :url, :string, null: false\r\n  add :secret, :uuid, null: false, default: fragment(\"uuid_generate_v4()\")\r\n\r\n  timestamps\r\nend\r\n```\r\n```elixir\r\nschema \"clients\"  do\r\n  field :name, :string\r\n  field :url, :string\r\n  field :secret, Ecto.UUID, read_after_writes: true\r\n  timestamps\r\nend\r\n```\r\n\r\nMy assumption was that creating a new record without specifying a 'secret' would let the database default kick in. Instead, here is what happens:\r\n\r\n```elixir\r\nchangeset = Client.changeset(%Client{}, %{name: \"local\", url: \"http://localhost:4200\"})\r\nRepo.insert(changeset)\r\n# => ** (Postgrex.Error) ERROR (not_null_violation): null value in column \"secret\" violates not-null constraint\r\n```\r\n\r\nI suppose I would have expected Ecto to only insert values that it had data for. However, Ecto is explicitly inserting `NULL`, so database defaults aren't kicking in. I'm thinking that changing this so that only non-null values are inserted is the appropriate fix for this.\r\n\r\nWhat do you guys think here? I'm sort of a newbie to Elixir, so feedback is appreciated."},"comment":{"url":"https://api.github.com/repos/elixir-lang/ecto/issues/comments/100593269","html_url":"https://github.com/elixir-lang/ecto/pull/591#issuecomment-100593269","issue_url":"https://api.github.com/repos/elixir-lang/ecto/issues/591","id":100593269,"user":{"login":"josevalim","id":9582,"avatar_url":"https://avatars.githubusercontent.com/u/9582?v=3","gravatar_id":"","url":"https://api.github.com/users/josevalim","html_url":"https://github.com/josevalim","followers_url":"https://api.github.com/users/josevalim/followers","following_url":"https://api.github.com/users/josevalim/following{/other_user}","gists_url":"https://api.github.com/users/josevalim/gists{/gist_id}","starred_url":"https://api.github.com/users/josevalim/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/josevalim/subscriptions","organizations_url":"https://api.github.com/users/josevalim/orgs","repos_url":"https://api.github.com/users/josevalim/repos","events_url":"https://api.github.com/users/josevalim/events{/privacy}","received_events_url":"https://api.github.com/users/josevalim/received_events","type":"User","site_admin":false},"created_at":"2015-05-10T06:38:35Z","updated_at":"2015-05-10T06:38:35Z","body":"Yes, this should be fixed and work in master. :)"}}