Hi, I have a “Trades” table which references the same table “Currency”, twice.
So there’s a table “currencies” with “USD”, “EUR”, “GBP”, etc.
And every forex trade has a “bought” column, referencing "currencies, a “sold” column which also references “currencies”, an exchange rate and a timestamp.
use Ecto.Migration
def change do
create table(:trades) do
add :bought, references(:currencies)
add :sold, references(:currencies)
add :rate, :float
timestamps()
end
end
What I don’t know (sorry for the noob question) is how to code the schema for this association on the two Ecto schemas I have, nor how to set the foreign keys here.
If someone can help out or point me to some good article on the issue I’d be very grateful.
Works if I change the columns in the migration to:
def change do
create table(:trades) do
add :bought_id, references(:currencies)
add :sold_id, references(:currencies)
add :rate, :float
timestamps()
end
end
That works nicely but on running Repo.get, it says:
__meta__: #Ecto.Schema.Metadata<:loaded, "trades">,
bought: #Ecto.Association.NotLoaded<association :bought is not loaded>,
bought_id: 1,
id: 1,
inserted_at: ~N[2020-11-09 19:53:04],