[Proposal] Add "into" keyword to ecto query expression

86 views
Skip to first unread message

Yusuf

unread,
Aug 31, 2022, 11:54:03 AM8/31/22
to elixir-lang-core
Hello everyone,

This is my first time posting, please excuse me if I did something wrong :)

I am writing a query like below,

query =
    from u in User,
    where: u.id in ^user_ids,
    select: {u.id, u}

data =
    query
    |> Repo.all()
    |> Enum.into(%{})

As follows, Does it make sense to add the "into" keyword into to this query?

query =
    from u in User,
    where: u.id in ^user_ids,
    select: {u.id, u},
    into: %{}

data =
    query
    |> Repo.all()


I would like your thoughts on this one.

Andrey Yugai

unread,
Aug 31, 2022, 12:20:28 PM8/31/22
to elixir-l...@googlegroups.com
I wouldn't claim this is a good idea because `into` in your example looks like leakage of app logic into DB level. If you're only ought to create user id - user map, it is pretty easy to do in Elixir from just a list of users. If you concerned about memory/time, you could try streaming from repo. If your data is inevitably key value table, then maybe you better off using JSONB column for storing maps, or even exploring document DBs.




-------- Original Message --------
--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/0e57204b-302b-494e-bcce-e0d1776218a8n%40googlegroups.com.

Daniel Kukuła

unread,
Aug 31, 2022, 12:54:22 PM8/31/22
to elixir-lang-core
remove the select to just have the user schema returned and then:
Repo.all |> Map.new(&{&1.id, &1})
Message has been deleted

Yusuf

unread,
Sep 1, 2022, 11:33:45 AM9/1/22
to elixir-lang-core

Thank you for your answers. 

You are right. 
31 Ağustos 2022 Çarşamba tarihinde saat 19:54:22 UTC+3 itibarıyla danie...@gmail.com şunları yazdı:

Greg Rychlewski

unread,
Sep 1, 2022, 12:28:26 PM9/1/22
to elixir-lang-core
Instead of `:into` it might be possible to allow selecting maps where the keys are column values. It could be worth exploring that idea.
Reply all
Reply to author
Forward
0 new messages