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!

First Post!

1player

1player

What’s the error? Works for me, but used like you did gives you a error that says “queries that do not have a schema need to explicitly pass a :select clause in query”.

Also you might need the ^ operator in front of that constant "ACME" string.

Try this:

schema_name = "some_table"
Test.Repo.all(from a in schema_name, where: a.name == ^"ACME", select: a.id)

EDIT: by the way, do you mean Ecto schema (which is actually a table) or PostgreSQL schema (which is a different problem and answer altogether)? I assume it’s the former.

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

Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
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
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

Other popular topics Top

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 records...
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44265 214
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement