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?
Most Liked
kingdomcoder
Schemaless changeset does not support inputs_for (GitHub Issue).
Please, use embedded schemas. This cost me 3 weeks of work.
redrapids
We should probably move Programming Phoenix LiveView to embedded, and talk about the tradeoffs.
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])
…
Popular in Questions
Other popular topics
Latest Phoenix Threads
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security









