Skip to content

Commit

Permalink
insert_all with query was not using field source (#4433)
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-rychlewski committed Jun 17, 2024
1 parent a92d8f8 commit 6f45d02
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 8 additions & 0 deletions integration_test/cases/repo.exs
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,14 @@ defmodule Ecto.Integration.RepoTest do

assert %Post{title: ^expected_title} = TestRepo.get(Post, expected_id)
end

test "insert_all with query and source field" do
%{id: post_id} = TestRepo.insert!(%Post{})
TestRepo.insert!(%Permalink{url: "url", title: "title"})

source = from p in Permalink, select: %{url: p.title, post_id: ^post_id}
assert {1, _} = TestRepo.insert_all(Permalink, source)
end
end

@tag :invalid_prefix
Expand Down
4 changes: 2 additions & 2 deletions lib/ecto/repo/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ defmodule Ecto.Repo.Schema do
%Ecto.Query.SelectExpr{expr: {:%{}, _ctx, args}} ->
Enum.map(args, fn {field, _} ->
case dumper do
%{^field => {_, _, false}} -> field
%{^field => {source, _, false}} -> source
%{} -> raise ArgumentError, "cannot select unwritable field `#{field}` for insert_all"
nil -> field
end
Expand All @@ -128,7 +128,7 @@ defmodule Ecto.Repo.Schema do
%Ecto.Query.SelectExpr{take: %{^ix => {_fun, fields}}} ->
Enum.map(fields, fn field ->
case dumper do
%{^field => {_, _, false}} -> field
%{^field => {source, _, false}} -> source
%{} -> raise ArgumentError, "cannot select unwritable field `#{field}` for insert_all"
nil -> field
end
Expand Down

0 comments on commit 6f45d02

Please sign in to comment.