fireproofsocks

fireproofsocks

Mutating the shape of data stored in the db vs. what's presented in Ecto struct (without embedded schema)

This is more of a educational question… usually, my Ecto schemas match the column names in my database pretty closely. Every now and then I specify the :source option in my field definitions if I want my Ecto schema to use a different field name than what’s in the database.

But how flexible is Ecto when you want your Ecto struct to be very different from what’s in the database. For example, let’s say we want a struct like this:

@type t :: %__MODULE__{
  a: String.t()
  b: integer()
  c: boolean()
}

but our database is actually defined like this:

defmodule Foo do
    use Ecto.Schema
    
    @primary_key false
    schema "bar" do
      field(:a, :string, null: false)
      field(:meta, :map)
    end  
  end

The idea being that the b and c values are persisted inside the meta map. Is this possible in Ecto? (And I realize that deviating too much between the 2 representations here can be confusing/unhelpful). But I’m curious – can it be done and how? Thanks in advance!

Most Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Ecto is pretty modular and so in some sense anything is possible if you call back to Repo.query! and Repo.load and do transformations yourself.

In particular however if you plan on inserting or updating data from Elixir into that table I would definitely recommend that your schema map pretty closely to the table. In your case there is no way for ecto to do an update based that changes what’s inside meta with a schema that treats those keys as virtual fields.

If you’re just trying to have more ergonomic ways to read or query data then you could explore using a database view and an Ecto Schema that maps tightly to that view, or even just a good old ecto query where you then transform the result into some other struct.

Where Next?

Popular in Questions Top

nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
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
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
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

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
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
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
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
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

We're in Beta

About us Mission Statement