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 (https://hexdocs.pm/ecto/Ecto.Schema.html#module-reflection)

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

JorisKok
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
joaquinalcerro
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 record...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
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
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New

We're in Beta

About us Mission Statement