Hi,
I would like to check if its possible to dynamcally update schema name in every Ecto.Schema queries.
Basically If we have couple of schema hosted on same database, and would like to manage one Connection pool(username and password, and database are same). But diffrent database schemas, we would like to access(every table exists across multiple schemas). So can i manage one schema structure in code and do queries/updates across multiple schemas.
That sounds like you’re conflating Ecto.Schema
with what is called a schema
in postgres.
A postgres schema
is a way to group tables within a database. Ecto being a database agnostic tool for querying e.g. sql databases allows you to use schemas by what is called prefix
. Those can be set in various places when building queries.
Ecto.Schema
are about defining a datatype (elixir struct) for certain sets of data you query from the db, often from a single table with additional features and setup related specifically of how that data is fetched, mapped, transformed coming from the database or being saved to the database.
1 Like
Hey @abhir2020 if what you’re asking is if you can use the same ecto Schema
module across different postgres schemas then sure, that’s what the prefix
option is for. eg:
user_from_schema1 = User |> where(foo: ^value) |> Repo.one!(prefix: "schema_1")
user_from_schema2 = User |> where(foo: ^value) |> Repo.one!(prefix: "schema_2")