tanweerdev

tanweerdev

Dynamic embedded schema based on type

I have a json/map field in database which can actually holds 3 kinds of objects based on type field define in table

so structure is like this

field(:type, :string) => user/player/admin
field(:object, :map) => embedded schema needed here. and it can be any of the three schemas ie user/admin/player

So is this possible I can have the flexibility of mysql and beauty/support of Ecto embeds.

Marked As Solved

mathieuprog

mathieuprog

Also Liked

mathieuprog

mathieuprog

That’s one way to achieve it. If the frontend calls the same service for handling different kind of data, then it makes sense that it is included by the frontend. Otherwise you may also pick the right embed on the server.

Note also that it is hard or probably impossible to have the structure that you have shown, i.e. have the type stored only in a separate field. That’s because when using a custom Ecto Type for the field (storing the dynamic embed), you do not have access to the other fields of the schema. So for example, if you want to load the data from the DB, in order to load the right embed struct you really need to have that type field living with your data.

Last Post!

mathieuprog

mathieuprog

That’s one way to achieve it. If the frontend calls the same service for handling different kind of data, then it makes sense that it is included by the frontend. Otherwise you may also pick the right embed on the server.

Note also that it is hard or probably impossible to have the structure that you have shown, i.e. have the type stored only in a separate field. That’s because when using a custom Ecto Type for the field (storing the dynamic embed), you do not have access to the other fields of the schema. So for example, if you want to load the data from the DB, in order to load the right embed struct you really need to have that type field living with your data.

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
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
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
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
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40082 209
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement