Assistance with Ecto, embedded and tables

Alchemists,

I’m pulling my hair out and thought I would come to this awesome community for assistance.

I’ve created a map (structs) which looks like the following:

%Trace{attributes: [%Attribute{key: "RequestedAmount",
		   type: "float", value: "20000.0"}, ...],
                              events: [%Event{attributes: [%Attribute{key: "AnotherKey"
						, type: "string", value: "hello!"}
									]
							},
					 ...]
	}

I’m wanting to persist this to my PostgreSQL database. I’ve come up with the following schema:

defmodule XesParser.Trace do
	use Ecto.Schema

	schema "traces" do
		embeds_many :attributes, Attribute
		embeds_many :events, Event 
	end
end

defmodule XesParser.Attribute do
	use Ecto.Schema

	schema "attributes" do
		field :type
		field :key
		field :value
	end
end

defmodule XesParser.Event do
	use Ecto.Schema

	schema "events" do
		embeds_many :attributes, Attribute
	end
end

I have two questions:

  1. What table(s) should I create in the migration?
  2. How do I insert this into the database, once the migration is done? I’m guessing something like:
trace = Repo.insert!(%Trace{})
		changeset = Ecto.Changeset.change(trace)
		changeset = Ecto.Changeset.put_embed(changeset, example.attributes, :attributes)
		changeset = Ecto.Changeset.put_embed(changeset, example.events, :attributes)

where the above is example, however, how do you manage the fact that events have attributes?

Any assistance would be wildly appreciated…

M

1 Like