jjabba
How do I load ecto structs from a postgresql function?
I’ve defined a table in postgresql to store a NonBinarySearchTree. The table is named nodes.
Each row contains an id, and optionally a parent_id (as a self referencing foreign key).
For ease and convenience i’ve defined two postgresql functions all_ancestors_of(id) and all_descendants_of(id)
The migration/definition of the ancestors-one looks like this
def up do
execute """
create function all_ancestors_of(node_id UUID) returns SETOF nodes AS
'WITH RECURSIVE ancestors AS (
SELECT
*
FROM
nodes
WHERE
nodes.id = all_ancestors_of.nodes_id
UNION
SELECT
parents.*
FROM
nodes AS parents
INNER JOIN ancestors a ON a.parent_id = parents.id
) SELECT * from ancestors where id != all_ancestors_of.node_id;
' LANGUAGE SQL;
"""
end
This works beautifully in SQL, but Im struggling to use these functions via Ecto.
How do I get the list of nodes (i have a schema for them) without resorting to
res = Ecto.Adapters.SQL.query!(Repo, "SELECT * from all_ancestors_of($1)", [uuid])
Enum.map(res.rows, &Repo.load(Node, {res.columns, &1}))
?
I’m was hoping to be able to do something like
query = from e in fragment(all_ancestors_of($1), [uuid])
nodes = Repo.all(query)
First Post!
Last Post!
thousandsofthem
Some (partially outdated) info:
UPDATE: for Ecto 3 you can use
__MODULE__.load(model, fields)
0
Popular in Questions
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
If I have a post route which an argument:
post /my_post_route/:my_param1, MyController.my_post_handler
How would get the post params ...
New
What is the idiomatic way of matching for not nil in Elixir?
E.g.,
First way:
defp halt_if_not_signed_in(conn, signed_in_account) when...
New
Credo is smart enough to check for (something like) this:
assert length(the_list) == 0
with this response:
Checking if an enum is empt...
New
I tried installing
elixir 1.11.2
erlang 23.3.4
via asdf in my zsh shell. Enabled the versions locally and globally.
When I list them ...
New
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
Hi!
Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
Other popular topics
Hi,
I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Hello, how can I check the Phoenix version ?
Thanks !
New
Hi there,
I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
Hello!
Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
Phoenix 1.4.0 released
Phoenix 1.4 is out! This release ships with exciting new features, most notably
with HTTP2 support, improved deve...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security









