-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support select: map(x, fields), select_merge: %{...}
in source query for insert_all
#4430
Comments
Please take a stab at it. It may be that making it work is just too complex, and then we could improve the error message, but we should definitely try it! |
I have made a PR which adds a test and the simplest code to make the test pass. As far as I can see the test (as others in the same vein) only assert that a query would be successfully created, not that it would be correct. Is there a way to assert on the query itself? |
Using insert_all also fails with # This does not work!
def test_1() do
# initial query
query = from(c in Click, select: c, limit: 1)
Ecto.Multi.new()
|> Ecto.Multi.insert_all(:click, Click, query)
|> Repo.transaction()
end
# This does not work!
def test_2() do
# initial query
query = from(c in Click, select: struct(c, [:short_code]), limit: 1)
Ecto.Multi.new()
|> Ecto.Multi.insert_all(:click, Click, query)
|> Repo.transaction()
end
# This does not work!
def test_3(short_code_override \\ "default sc") do
# initial query
query = from(c in Click, select: %{c | short_code: ^short_code_override}, limit: 1)
Ecto.Multi.new()
|> Ecto.Multi.insert_all(:click, Click, query)
|> Repo.transaction()
end |
Hi @aglassman , Something like this will not work because the update syntax is essentially post processing. Whatever is returned from the query will later be updated in Elixir. Thus it can't work for query = from(c in Click, select: %{c | short_code: ^short_code_override}, limit: 1) But this should work. I tested it: query = from(c in Click, select: struct(c, [:short_code]), limit: 1) Maybe your ecto version is old? If you try on the latest commit does it still happen? |
I thought maybe it would make sense to select the entire struct. But after playing around with it there are quite a few issues. Mostly because you will be selecting primary keys and foreign keys which will cause issues in a lot of cases. |
@greg-rychlewski Thanks for the explanation into why the update syntax does not work, that is good to know. As for |
Elixir version
1.16.3
Database and Version
PostgreSQL 15.0
Ecto Versions
master
Database Adapter and Versions (postgrex, myxql, etc)
postgrex
Current behavior
works as intended.
raises an ArgumentError stating that the source query does not select into a map.
Expected behavior
The above two snippets should both be supported as they should result in the same source query being produced.
I would be happy to take a stab at this if this is indeed the intended behaviour.
The text was updated successfully, but these errors were encountered: