ecto icon indicating copy to clipboard operation
ecto copied to clipboard

validate_required/2 and embeds_many auto generated IDs

Open robgunnatkin opened this issue 10 years ago • 2 comments

Environment

  • Elixir version: Elixir 1.2.1
  • Database: PostgreSQL 9.4.5.0
  • Ecto version: Ecto 2.0.0
  • Database adapter: postgrex 0.11.1
  • Operating system: OS X 10.11.3

Current behavior

validate_required/2 ignores the id field that is auto generated for embedded structs.

defmodule Example.Room do
  use Example.Web, :model

  schema "rooms" do
    field :room_name, :string, null: false
    field :active, :boolean, default: true, null: false
    field :description, :string, null: false
    embeds_many :packages, Example.Package
    timestamps
  end

defmodule Example.Package do
  use Example.Web, :model

  embedded_schema do
    field :description, :string
    field :price, :integer
  end

Ecto, by default, will auto generate an id field and create a UUID for new Example.Package.

%Example.Package{__meta__: #Ecto.Schema.Metadata<:loaded>,
   description: "T-Shirt", id: "9b724297-a41b-44d1-a546-d19c482d37ea",
   price: 12}

When cast_embed/2 is called in the parent struct, for example:

  def example_changeset(room, params \\ %{}) do
    room
    |> cast(params, ~w(room_name description active))
    |> validate_required(~w(name description active packages)a)
    |> cast_embed(:packages)
  end

The parameters for the given embed will be retrieved from changeset.params and the changeset function in the embed module will be invoked. So cast_embed will use Example.Package.changeset/2 to do create a changeset for the embedded struct(s):

  def changeset(package, params \\ %{}) do
    package
    |> cast(params, ~w(id description price))
    |> validate_required(:id)
  end

One would expect a changeset with changeset.error when params is missing an id field. However the following RuntimeError is returned:

** (RuntimeError) you are attempting to change relation :packages of                                                            Merlin.Room, but there is missing data.                                                                                                
If you are attempting to update an existing entry, please make sure
you include the entry primary key (ID) alongside the data.

If you have a relationship with many children, at least the same N
children must be given on update. By default it is not possible to
orphan embed nor associated records, attempting to do so results in
this error message.

It is possible to change this behaviour by setting `:on_replace` when
defining the relation. See `Ecto.Changeset`'s section on related data
for more info.

    (ecto) lib/ecto/changeset/relation.ex:177: Ecto.Changeset.Relation.on_replace/2
    (ecto) lib/ecto/changeset/relation.ex:282: Ecto.Changeset.Relation.reduce_delete_changesets/5
    (ecto) lib/ecto/changeset.ex:512: Ecto.Changeset.cast_relation/4

Expected behavior

Return a invalid changeset with proper changeset.error when required id field is expected but not present in params for an embedded struct.

robgunnatkin avatar Mar 08 '16 02:03 robgunnatkin

You are right. I will have to check exactly how to fix this though but it will be done before 2.0. :)

josevalim avatar Mar 08 '16 12:03 josevalim

Hi @jrobn! I have investigating this and I believe you try setting your on_replace: :mark_as_invalid. The issue is that the on_replace checks run before calling the changeset and I am afraid that always calling the changeset function before on_replace can expose the security issues it aims to address. With on_replace: :mark_as_invalid, missing IDs should automatically be marked as such, which seems to address your concerns.

Thank you!

josevalim avatar Apr 16 '16 16:04 josevalim