Reference a :conflict_target of a nested association while Using Repo.insert?

Given the following Ecto schema struct with a nested association:

blah = 
  %Foo{
    ...
    bars: [
      %Bar{
        ...
      },
      %Bar{
        ...
      }
    ]
  }

How can I reference a conflict target constraint in the blah.bars schema?

Repo.insert(Foo, blah, 
          on_conflict: :replace, 
          conflict_target: {:constraint, :bar_unique_contraint_name}
        )

This code obviously doesn’t work because it looks for the constraint named “bar_unique_contraint_name” in the “Foo” schema. What I would like it to do is look for that constraint in the nested foo.bars “Bar” schema during the insert.

2 Likes

the solution you are looking for maybe in this github issue thread: https://github.com/elixir-ecto/ecto/issues/1907