why

why

Dynamic schema name

Hello,

I’m trying to dynamically pass a schema name into a function for use in a query. Can’t quite figure out in which format to pass in the schema name.

Pseudo code (not working):

def test(schema_name) do
  Test.Repo.all(from a in schema_name, where: a.name == "ACME")
end

Have tried various ways of passing the schema_name in, including in the form of a {“source”, Schema} tuple, as discussed here: https://stackoverflow.com/questions/40687186/how-can-you-dynamically-set-a-schema-table-name-for-a-ecto-model

But no luck so far… Any ideas how one could do this?

Thank you!

Most Liked

1player

1player

Sorry for the double post, but using the schema module actually works!

Your problem is using the Account atom, which isn’t probably aliased to its fully qualified name.

You’ll see that if you alias it, or use the fully qualified module name, such as YourApp.Data.Account, it will work.

alias YourApp.Data.Account

# Without the alias above, Ecto does not know what Account is. 
# You might have multiple Account modules in different namespaces.
test(Account)

# This will always work, alias or not
test(YourApp.Data.Account)
1player

1player

Ah, gotcha.

The use Ecto.Schema macro creates a __schema__ function that exposes some introspection details about your schema, and you can use that to find the actual table name as string (Ecto.Schema — Ecto v3.14.0)

def test(schema_name) when is_atom(schema_name) do
  # Get the table name as string
  table_name = schema_name.__schema__(:source)
  Test.Repo.all(from a in table_name, where: a.name == "ACME", select: a.id)
end

Hope this helps!

why

why

Awesome, thanks a lot, that fixed it, it was the aliasing :slightly_smiling_face:

Great help, really appreciate it!

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Darmani72
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
fayddelight
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
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130579 1222
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31525 112
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44608 311
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement