bodhilogic
Problem with Ecto prefix: usage
I am attempting to use the prefix: attribute in my queries so that I can control which mySQL database I am reading/writing from.
When I attempt a Repo.get_by() call, Elixir moans that t.prefix doesn’t exist (or something similar to that) - Ecto thinks the prefix: attribute is a table field (column) and not the indication of which database to query.
Repo.get_by(Ticket, ticket: ticket_params[:ticket], prefix: "other_db")
The prefix: attribute seems(?) to work okay with other query types like Repo.insert!(prefix: "other_db") or Repo.get!(Ticket, id, prefix: "other_db") or Repo.all(prefix: "other_db"), etc.
What am I doing wrong?
Marked As Solved
benwilson512
You need to wrap the parameters you’re getting by in a list instead of using the last arg syntax sugar:
Repo.get_by(Ticket, [ticket: ticket_params[:ticket]], prefix: "other_db")
What you have is being interpreted as:
Repo.get_by(Ticket, [ticket: ticket_params[:ticket], prefix: "other_db"])
Also Liked
fmcgeough
ah..right.. so the 2nd param is a Keyword. so prefix: “other_db” is just another element in the 2nd param Keyword List.
bodhilogic
I created another server and in the process was able to run my changed code.
Bracketing the query parameters as suggested by benwilson512 worked a treat!!
Thanks.
Popular in Questions
Other popular topics
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
- #javascript
- #code-sync
- #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
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








