Hello!
I am trying to create a many_to_many relation between two models in my app. But I cant seem to get it to work.
I want to have many special rules objects for each equipment model in my app, so I have these models:
defmodule App.Equipment do
use App.Web, :model
schema "equipment" do
field :range, :integer
field :name, :string
field :strength, :integer
many_to_many :special_rules, App.SpecialRules, join_through: App.SpecialRulesList
timestamps()
end
end
defmodule App.SpecialRule do
use App.Web, :model
schema "special_rules" do
field :description, :string
field :type, :integer
field :name, :string
many_to_many :equipment, App.Equipment, join_through: App.SpecialRulesList
timestamps()
end
end
defmodule App.SpecialRulesList do
use App.Web, :model
schema "special_rules_list" do
belongs_to :special_rules, App.SpecialRule
belongs_to :equipment, App.Equipment
timestamps
end
end
And these are my tables:
defmodule App.Repo.Migrations.CreateEquipment do
use Ecto.Migration
def change do
create table(:equipment) do
add :range, :integer
add :name, :string
add :strength, :integer
end
end
defmodule App.Repo.Migrations.CreateSpecialRule do
use Ecto.Migration
def change do
create table(:special_rules) do
add :description, :text
add :type, :integer
add :name, :string
end
end
defmodule App.Repo.Migrations.CreateEquipmentSpecialRuleJoin do
use Ecto.Migration
def change do
create table(:special_rules_list, primary_key: false) do
add :equipment_id, references(:equipment)
add :special_rules_id, references(:special_rules)
timestamps()
end
end
When i try to load the relation with preload i the equipment controller i have
defmodule App.EquipmentController do
use App.Web, :controller
alias App.Equipment
alias App.SpecialRule
def index(conn, _params) do
equipment = Repo.all(Equipment) |> Repo.preload([:special_rules])
render(conn, "index.json", equipment: equipment)
end
end
But I always get some strange error, like (ERROR 42P01 (undefined_table): relation “special_rules_list” does not exist) does not exists or table does not exists (depending on how i load thing in the controller or present the data in the render method).
I run mix ecto.reset && mix phoenix.server
whenever i do changes in the models or tables.
What am I doing wrong? Feels like there is some small detail I have missed.