themarlzy

themarlzy

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

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 55125 245
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement