postgresql - Column not found when seeding database with structs with has_many relationship -
i first tried seeding database simple 1 level deep structs , works great. however, when add in path mapping match top level structs paths field error.
** (postgrex.error) error 42703 (undefined_column): column "apis_id" of relation "paths" not exist
i've tried removing apis_id believe need field preload path struct in apicontroller. perhaps problem lies in has_many
/belongs_to
syntax although believe have correct , i've tried swamping out other foreign_keys , references no luck.
i missing stupid , need fresh eyes, i'm hoping understanding of relationship possibly messed up.
here migrations:
_create_api.exs
defmodule app.repo.migrations.createapi use ecto.migration def change create table(:apis) add :basepath, :string add :definitions, :string add :paths, references(:paths) timestamps() end end end
_create_paths.exs
defmodule app.repo.migrations.createpath use ecto.migration def change create table(:paths) add :apis_id, :string add :route, :string timestamps() end end end
my models: api.ex
defmodule app.api use app.web, :model schema "apis" field :basepath, :string field :definitions, :string has_many :paths, app.path, foreign_key: :apis_id timestamps() end end
path.ex
defmodule app.path use app.web, :model schema "paths" belongs_to :apis, app.api, references: :apis_id field :route, :string timestamps() end end
also here apicontroller api_controller.ex
defmodule app.apicontroller use app.web, :controller alias app.api def index(conn, _params) apis = repo.all(api) |> repo.preload([:paths]) render conn, "index.json", apis: apis end end
and 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 rip out %path mapping , make empty string.
any of steering in correct direction appreciated!
thank in advance.
you shouldn't put reference paths
database. relation one-to-many 1 sided - need api_id
inside path
schema, don't need other way around.
however ecto
able map path
s field inside api
struct. don't need field paths
inside _create_api
migration.
so believe correct file contents should be:
_create_api.exs
defmodule app.repo.migrations.createapi use ecto.migration def change create table(:apis) add :basepath, :string add :definitions, :string timestamps() end end end
_create_paths.exs
defmodule app.repo.migrations.createpath use ecto.migration def change create table(:paths) add :api_id, references(:apis) add :route, :string timestamps() end end end
api.ex
defmodule app.api use app.web, :model schema "apis" field :basepath, :string field :definitions, :string has_many :paths, app.path timestamps() end end
path.ex
defmodule app.path use app.web, :model schema "paths" belongs_to :api, app.api field :route, :string timestamps() end end
please note removed references
, foreign_key
models. changed name of field apis_id
api_id
match standard naming.
edit: added seeds.exs
alias app.repo alias app.path alias app.api api = repo.insert! %api{ basepath: "v1", definitions: "none" } path = repo.insert! %path{ api_id: api.id, route: "/account" }
but advise use changesets.
Comments
Post a Comment