I first tried seeding my database with a simple one level deep structs and it works great. However, when I add in a Path mapping to match my top level structs paths field then I get the error.
** (Postgrex.Error) ERROR 42703 (undefined_column): column "apis_id" of relation "paths" does not exist
I've tried removing the apis_id but I believe I need that field to preload the Path struct in my ApiController. Perhaps the problem lies in my has_many/belongs_to syntax although I believe I have it correct and I've tried swamping out other foreign_keys and references with no luck.
I could also be just missing something really stupid and need some fresh eyes, but I'm hoping my understanding of this relationship is possibly messed up.
Here are my migrations:
_create_api.exs
defmodule App.Repo.Migrations.CreateApi do
use Ecto.Migration
def change do
create table(:apis) do
add :basePath, :string
add :definitions, :string
add :paths, references(:paths)
timestamps()
end
end
end
_create_paths.exs
defmodule App.Repo.Migrations.CreatePath do
use Ecto.Migration
def change do
create table(:paths) do
add :apis_id, :string
add :route, :string
timestamps()
end
end
end
My models: api.ex
defmodule App.Api do
use App.Web, :model
schema "apis" do
field :basePath, :string
field :definitions, :string
has_many :paths, App.Path, foreign_key: :apis_id
timestamps()
end
end
path.ex
defmodule App.Path do
use App.Web, :model
schema "paths" do
belongs_to :apis, App.Api, references: :apis_id
field :route, :string
timestamps()
end
end
Also here is my ApiController api_controller.ex
defmodule App.ApiController do
use App.Web, :controller
alias App.Api
def index(conn, _params) do
apis = Repo.all(Api) |> Repo.preload([:paths])
render conn, "index.json", apis: apis
end
end
And finally my seeds.ex
alias App.Repo
alias App.Path
alias App.Api
api = %Api{
basePath: "v1",
definitions: "none",
paths: [
%Path{
apis_id: 1,
route: "/account"
}
]
}
Repo.insert! api
This works if I rip out the %Path mapping and make it an empty string.
Any help of steering in the correct direction would be greatly appreciated!
Thank you in advance.