{"action":"opened","issue":{"url":"https://api.github.com/repos/elixir-ecto/ecto/issues/3097","repository_url":"https://api.github.com/repos/elixir-ecto/ecto","labels_url":"https://api.github.com/repos/elixir-ecto/ecto/issues/3097/labels{/name}","comments_url":"https://api.github.com/repos/elixir-ecto/ecto/issues/3097/comments","events_url":"https://api.github.com/repos/elixir-ecto/ecto/issues/3097/events","html_url":"https://github.com/elixir-ecto/ecto/issues/3097","id":488126275,"node_id":"MDU6SXNzdWU0ODgxMjYyNzU=","number":3097,"title":"Cannot delete `embeds_many` items nested within `embeds_one`","user":{"login":"peterhorne","id":1069142,"node_id":"MDQ6VXNlcjEwNjkxNDI=","avatar_url":"https://avatars0.githubusercontent.com/u/1069142?v=4","gravatar_id":"","url":"https://api.github.com/users/peterhorne","html_url":"https://github.com/peterhorne","followers_url":"https://api.github.com/users/peterhorne/followers","following_url":"https://api.github.com/users/peterhorne/following{/other_user}","gists_url":"https://api.github.com/users/peterhorne/gists{/gist_id}","starred_url":"https://api.github.com/users/peterhorne/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/peterhorne/subscriptions","organizations_url":"https://api.github.com/users/peterhorne/orgs","repos_url":"https://api.github.com/users/peterhorne/repos","events_url":"https://api.github.com/users/peterhorne/events{/privacy}","received_events_url":"https://api.github.com/users/peterhorne/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2019-09-02T10:47:06Z","updated_at":"2019-09-02T10:47:06Z","closed_at":null,"author_association":"NONE","body":"### Precheck\r\n\r\n* Do not use the issues tracker for help or support requests (try Elixir Forum, Stack Overflow, IRC or mailing lists, etc).\r\n* For proposing a new feature, please start a discussion on [elixir-ecto](https://groups.google.com/forum/#!forum/elixir-ecto).\r\n* For bugs, do a quick search and make sure the bug has not yet been reported.\r\n* Finally, be nice and have fun!\r\n\r\n### Environment\r\n\r\n* Elixir version (elixir -v):\r\n> Erlang/OTP 22 [erts-10.4.4] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [hipe] [dtrace]\r\n> \r\n> Elixir 1.7.3 (compiled with Erlang/OTP 19)\r\n\r\n* Database and version (PostgreSQL 9.4, MongoDB 3.2, etc.):\r\n> (PostgreSQL) 11.5\r\n\r\n* Ecto version (mix deps):\r\n> * ecto (Hex package) (mix)\r\n>   locked at 3.1.7 (ecto) fa21d06e\r\n>   the dependency build is outdated, please run \"mix deps.compile\"\r\n\r\n* Database adapter and version (mix deps):\r\n> * jason (Hex package) (mix)\r\n>   locked at 1.1.2 (jason) b03dedea\r\n>   the dependency build is outdated, please run \"mix deps.compile\"\r\n> * db_connection 2.1.1 (Hex package) (mix)\r\n>   locked at 2.1.1 (db_connection) a51e8a2e\r\n>   ok\r\n> * postgrex (Hex package) (mix)\r\n>   locked at 0.15.0 (postgrex) dd534916\r\n>   the dependency build is outdated, please run \"mix deps.compile\"\r\n> * ecto_sql (Hex package) (mix)\r\n>   locked at 3.1.6 (ecto_sql) 1e80e30d\r\n>   the dependency build is outdated, please run \"mix deps.compile\"\r\n\r\n* Operating system:\r\n> macOS 10.14.5 (18F132)\r\n\r\n### Current behavior\r\n\r\nGiven the following schema:\r\n\r\n```elixir\r\ndefmodule EmbedBug.Order do\r\n  use Ecto.Schema\r\n\r\n  schema \"orders\" do\r\n    field(:number, :integer)\r\n    embeds_one(:cart, EmbedBug.Cart, on_replace: :update)\r\n  end\r\nend\r\n\r\ndefmodule EmbedBug.Cart do\r\n  use Ecto.Schema\r\n\r\n  @primary_key false\r\n\r\n  embedded_schema do\r\n    field(:type, :integer)\r\n    embeds_many(:items, EmbedBug.Item, on_replace: :delete)\r\n  end\r\nend\r\n\r\ndefmodule EmbedBug.Item do\r\n  use Ecto.Schema\r\n\r\n  @primary_key false\r\n\r\n  embedded_schema do\r\n    field(:name)\r\n  end\r\nend\r\n```\r\n\r\nI am trying to update an `Order` to remove the embedded `items`:\r\n\r\n```elixir\r\n    {:ok, order} =\r\n      EmbedBug.Repo.insert(%EmbedBug.Order{\r\n        number: 1,\r\n        cart: %EmbedBug.Cart{type: 1, items: [%EmbedBug.Item{name: \"lala\"}]}\r\n      })\r\n\r\n    order2 =\r\n      order\r\n      |> Ecto.Changeset.change()\r\n      |> Ecto.Changeset.put_embed(:cart, %{items: []})\r\n\r\n    EmbedBug.Repo.update(order2)\r\n```\r\n\r\nThis raises the following:\r\n\r\n```\r\n** (ArgumentError) got action :delete in changeset for embedded EmbedBug.Item while inserting\r\n```\r\n\r\nInspecting `order2` I can see the changeset for `cart` has `action: :insert` while the nested `items` has `[{..., action: replace, changes: %{}}]`.\r\n\r\nI have created a repo with a test that exhibits this behaviour: https://github.com/peterhorne/ecto-embed-bug/blob/master/test/embed_bug_test.exs\r\n\r\n### Expected behavior\r\n\r\n`Repo.update` should successfully update `order` to remove the `items` nested inside `cart`. If `cart` is being inserted then `items` should be empty. Alternatively `cart` should `update` and `items` should remain as is."}}
{"actor":{"display_login":"peterhorne"}}