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?

Can you post Ecto version info from your mix.exs file?
and mysql library as well.

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"])
7 Likes

ah…right… so the 2nd param is a Keyword. so prefix: “other_db” is just another element in the 2nd param Keyword List.

2 Likes

I’ll give it a shot - what you are saying makes sense to me.

I’ve made the code changes but I can’t test it until tomorrow because my ‘process’ has already been run for today so my code won’t attempt to do anything.

I’ll let everyone know tomorrow (7 Mar 2019), if it worked or not.

      {:phoenix_ecto, "~> 3.2"},
      {:mariaex, ">= 0.0.0"},

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.

1 Like