NaN
I have multiple tables with the same structure. They are all located in the same db with different names, storing different data. How can I set the schema in a fn to reduce boilerplate? Am I going about this the wrong way?
The code below results in the following error:
(ArgumentError) cannot set attribute @ecto_schema_defined inside function/macro
defmodule NsMetrics.Name.Api do
use Ecto.Schema
import Ecto.Changeset
def set(db_name) do
schema db_name do
field(:date, :string)
field(:t1, :integer)
field(:t2, :integer)
field(:t3, :integer)
timestamps()
end
end
def changeset(line, attrs) do
line
|> cast(attrs, [:date, :t1, :t2, :t3])
|> validate_required([:date, :t1, :t2, :t3])
end
end
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
Hi all,
I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
Hi all, I’m new here and would like to hear peoples views on my Elixir/Phoenix based game-engine! :smiley:
Mutonex gamedesign was finish...
New
Do we have maintainer of absinthe-graphql/dataloader here? We have a PR that we’d like to merge to the library. Relatively small additive...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
LostKobrakai
You can use a single schema with multiple tables: Ecto.Schema — Ecto v3.14.0
NaN
Thank you for the reply. Im not sure this solves the problem for 2 reasons. Maybe Im missing it?
al2o3cr
Can you clarify on this? What specifically wouldn’t make sense?
A lot of what Ecto provides is at compile-time, so runtime-definition like you’re thinking of is going to be fighting with the tool quite hard.
NaN
Thank you for the question! Even if I define a master schema, creating a schema module per table and defining the associations does not make sense. There could be hundreds of them and new ones will be added as time goes on… but they all share the same structure.
dimitarvp
Then you should go for a custom
Ecto.Typethat is aware of the polymorphic pieces of data and properly branches on them to create the appropriate (different) Elixir structs.If you can give us an example of a master and e.g. two “child” associations I believe we would be able to give you a head-start.
NaN
It would look something like this, if Im understanding correctly. So Im clear about my understanding of the following code. This will create a shared instance between 2 tables with the same fields… If that understanding is misapprehended please let me know!
EDIT: Id like to add here there are neither nor will there ever be fields that deviate from the same structure.
dimitarvp
Still not quite clear. I don’t see the value add of the second table.
Can you also give a few examples of actual data inside the DB? Just do a few
selectqueries inpsqland paste us the result here (with anonymized / edited values if you like)?NaN
Its time series data from different sources. Putting them all in one table only stands to make handling the data arduous. They will all look the same as below.
dimitarvp
Are these the contents of the “fields_in_common” table, or “ad_hoc_*”?
NaN
ad_hoc_*there is no tablefields_in_commonits just that all the tables share all the same fields in common… Your question leads me to believe Im using it wrong.. ? Am I missing it?Thank you for working with me!
Im looking over the Ecto.Type docs to try to anticipate your solution..