Hello there!
I’m having a devil of a time getting a dynamic embedded schema to save in postgres.
So I started here, basically
But my core problem is that despite several iterations, when it goes to save it to the database, it throws
(UndefinedFunctionError) function :map.schema/1 is undefined (module :map is not available)
So in my hunt, I came across this package and it slimmed down the code, but I still have the same problem :(. It’s like ecto isn’t seeing the type update before it goes into schema. With your package the changeset automatically sets the type, which is really super nice, but for some reason I can’t get this to save to postgres:
## This is the latest iteration, of many attempts.
defmodule Geeks.Activities.Activity do
@moduledoc false
use Ecto.Schema
import Ecto.Changeset
import GatheringRole
alias Geeks.Activities.Types
embedded_schema do
field :type, ActivityType
field :title, :string
field :max_participants, :integer
field :next_start, :utc_datetime
embeds_one :properties, :map
embeds_many :rsvp, Geeks.Activities.ActivityRsvp
embeds_many :history, Geeks.Activities.ActivityHistory do
field :history_event, :string
timestamps([types: :utc_datetime])
end
timestamps([types: :utc_datetime])
end
@all_fields ~w(type title max_participants next_start)a
@required_fields ~w(type title)a
defp properties_type(props_schema) do
{:embed,
%Ecto.Embedded{
cardinality: :one,
field: :properties,
on_cast: &props_schema.changeset(&1, &2),
on_replace: :update,
owner: nil,
related: props_schema,
unique: true
}}
end
def create_changeset(%{ properties: props } = params) do
{:ok, props_schema} = Types.get_schema(:ttrpg)
props_cs = EctoMorph.generate_changeset(props, props_schema)
case props_cs.valid? do
true ->
changeset =
%__MODULE__{}
|> cast(params, @required_fields)
|> Map.update!(:types, &Map.put(&1, :properties, properties_type(props_schema)))
|> cast_embed(:properties)
|> validate_required(@required_fields)
false ->
props_cs
end
end
def create_changeset(%{} = params ) do
create_changeset(%{params | properties: %{}})
end
end
And elsewhere, in the context:
def add_activity(%Gathering{ activities: activities} = gathering, activity_params \\ %{properties: %{}}) do
changeset = Ecto.Changeset.change(gathering)
activity = Geeks.Activities.Activity.create_changeset(activity_params)
case activity.valid? do
true -> changeset = Ecto.Changeset.put_embed(changeset, :activities, activities ++ [activity])
|> Geeks.Repo.update!
false -> {:error, activity}
end
I did see this post: How to validate dynamic fields in embedded_schema - #4 by LostKobrakai
But it just seems like I’m missing something
I’m going to try to cross-post this to the elixir forums, but man it’s a brainweaver!