tomkonidas

tomkonidas

Schemaless Changesets vs Embedded Schemas for Phoenix Forms

While reading Programming Phoenix LiveView, I came across a section (Model Change with Schemaless Changesets - Chapter 5) which goes through setting up schemaless changesets for forms that you do not have a database table for.

I am very familiar with this method (I do it all the time), however instead of creating a Schemaless Changeset, I usually opt to just make an Embedded Schema. To me it seems simpler/cleaner to be able to define the struct and the type at the same time instead of maintaining two things (defstruct and types).


Schemaless

defmodule MyApp.Accounts.User do
  defstruct [:first_name, :email]
  @types %{first_name: :string, email: :string}
end

Embedded

defmodule MyApp.Accounts.User do
  @primary_key false  
  embedded_schema do
    field :first_name, :string
    field :email, :string
  end
end

Question:

Are there any reasons to choose schemaless changesets over embedded schemas?

First Post!

LostKobrakai

LostKobrakai

Schemaless changesets can be defined at runtime/by code. So it‘s useful when you don‘t have a fixed schema.

Most Liked

kingdomcoder

kingdomcoder

Schemaless changeset does not support inputs_for (GitHub Issue).

Please, use embedded schemas. This cost me 3 weeks of work.

redrapids

redrapids

Author of Adopting Elixir

We should probably move Programming Phoenix LiveView to embedded, and talk about the tradeoffs.

LostKobrakai

LostKobrakai

There‘s no need for it to be a module attribute though. The data can come from wherever:

{%{field: nil}, %{field: :integer}}
|> Ecto.Changeset.cast(params, [:field])
…

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
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
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
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
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

Other popular topics Top

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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
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
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement