Hello everyone.
I’ve been wondering if i’m taking the wrong approach.
I want to be able to create a new comment associated to an author by its unique author_name, but the foreign key assigned to comments is author_id. I want both of these unique values to work author_id
and author_name
, since they do not repeat.
My migrations are:
defmodule MyApp.Repo.Migrations.CreateAuthors do
use Ecto.Migration
def up do
create table(:authors) do
add(:last_name, :string)
add(:name, :string)
timestamps()
end
create unique_index(:authors, [:name])
end
end
defmodule MyApp.Repo.Migrations.CreateComments do
use Ecto.Migration
def up do
create table(:comments) do
add(:author_id, references(:authors, on_delete: :nothing))
add(:body, :string)
timestamps()
end
create(index(:comments, [:author_id]))
end
end
The create mutation I want to make with author_name instead of author_id:
mutation{
createComment(body: "Hello world!" author_name: "Anna"){
id
body
}
}
I’m looking for a double foreign key of some kind. Is there an easy approach in Ecto to this?