Custom query run in action

I'm wondering how to run a regular query (SELECT ...) in an action for example using Repo.query! šŸ¤” my attempts fail. Seems I can't use the Repo module in an action 😬
Solution:
I literally showed you how to do it. ```elixir action :activity_thismonth, :map do run fn input, -> """...
Jump to solution
15 Replies
ZachDaniel
ZachDaniel•3mo ago
In what context? a manual read? A generic action? How did your attempts fail? You can definitely use the repo module in an action šŸ˜„
ken-kost
ken-kostOP•3mo ago
hmm, generic action, okay for example if I have a resource with
postgres do
table "my_table"
repo MyApp.Repo
end
postgres do
table "my_table"
repo MyApp.Repo
end
and an action
action :activity_this_month, :map do
"""
SELECT * FROM my_table
"""
|> MyApp.Repo.query!()
end
action :activity_this_month, :map do
"""
SELECT * FROM my_table
"""
|> MyApp.Repo.query!()
end
I'm getting on mix compile
** (RuntimeError) could not lookup Ecto repo MyApp.Repo because it was not started or it does not exist
(ecto 3.13.2) lib/ecto/repo/registry.ex:22: Ecto.Repo.Registry.lookup/1
(ecto_sql 3.13.2) lib/ecto/adapters/sql.ex:622: Ecto.Adapters.SQL.query/4
(ecto_sql 3.13.2) lib/ecto/adapters/sql.ex:605: Ecto.Adapters.SQL.query!/4
lib/my_app/resources/my_resource.ex:40: (module)
** (RuntimeError) could not lookup Ecto repo MyApp.Repo because it was not started or it does not exist
(ecto 3.13.2) lib/ecto/repo/registry.ex:22: Ecto.Repo.Registry.lookup/1
(ecto_sql 3.13.2) lib/ecto/adapters/sql.ex:622: Ecto.Adapters.SQL.query/4
(ecto_sql 3.13.2) lib/ecto/adapters/sql.ex:605: Ecto.Adapters.SQL.query!/4
lib/my_app/resources/my_resource.ex:40: (module)
šŸ¤”
ZachDaniel
ZachDaniel•3mo ago
Your repo is not started is the repo in your application supervision tree?
ken-kost
ken-kostOP•3mo ago
it is tried it on another project, same error. You're saying the above should work? Can you try it please šŸ™ I also thought this should work but :/ omg wait nvm 😩
ZachDaniel
ZachDaniel•3mo ago
lol Two things: 1. generally on you to get a reproduction running šŸ˜„ 2. no it doesn't work
action :activity_this_month, :map do
run fn input, _ ->
"""
SELECT * FROM my_table
"""
|> MyApp.Repo.query!()
end
end
action :activity_this_month, :map do
run fn input, _ ->
"""
SELECT * FROM my_table
"""
|> MyApp.Repo.query!()
end
end
Your example was trying to use the repo at compile time So it wasn't started make sure to read the generic actions guide šŸ˜‰
Elixord
Elixord•3mo ago
Hexdocs Search Results
Searched ash-3.5.32 for generic actions
ash-3.5.32 | extras
ken-kost
ken-kostOP•3mo ago
alright :thinkies: wdym by
You can definitely use the repo module in an action
then? why is it being used at compile time? I thought it's used when called... I'm guessing I should do this sort of thing outside of an resource then, just a regular function call. šŸ¤”
ZachDaniel
ZachDaniel•3mo ago
šŸ¤” What exactly are you trying to do To describe what happens when a generic action is called, you provide a function to the run option like the code example I just showed you that is how you define a generic action. What you tried to do is the equivalent of attempting to use the repo module at compile time, while defining a generic action
ken-kost
ken-kostOP•3mo ago
I wanted to have an action that returns something custom related to that resource table, I'm doing some custom counting. I moved it to a function inside the resource and that works.
there are no special rules about how they work
I thought it should work but I guess not everything belongs in an action šŸ˜… thank you for helping šŸ™‡ to quote Socrates: all I know is that I know nothing šŸ›
ZachDaniel
ZachDaniel•3mo ago
@ken-kost you aren't understanding me lol It absolutely does work in a generic action you just typed your generic action wrong, thats all
Solution
ZachDaniel
ZachDaniel•3mo ago
I literally showed you how to do it.
action :activity_this_month, :map do
run fn input, _ ->
"""
SELECT * FROM my_table
"""
|> MyApp.Repo.query!()
end
end
action :activity_this_month, :map do
run fn input, _ ->
"""
SELECT * FROM my_table
"""
|> MyApp.Repo.query!()
end
end
ken-kost
ken-kostOP•3mo ago
omg 😵
ZachDaniel
ZachDaniel•3mo ago
Probably me saying
2. no it doesn't work
threw your for a loop, my bad šŸ˜„
ken-kost
ken-kostOP•3mo ago
yeah I thought this doesn't work: man... I should rest okay. at least I thought right. just missed the syntax
ken-kost
ken-kostOP•3mo ago
everything belongs in an action
No description

Did you find this page helpful?