drapermd

drapermd

Hello there!

I’m having a devil of a time getting a dynamic embedded schema to save in postgres.

So I started here, basically
https://github.com/elixir-ecto/ecto/pull/2113#issuecomment-412829999

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!

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews