Hi guys, first time posting on the forum so let me know if there’s anything I can improve on!
Currently the issue I’m having is inserting foreign keys into a repo. The Patient repo requires keys from the Gp, CDoctor, and Pharmacy repos to be inserted. Currently, a primary key is generated on each of the Gp, CDoctor and Pharmacy repos, and this value is then passed through assigns to the patient form. However upon submitting the form, errors appear in the changeset:
errors: [gp_id: {“is invalid”, [type: :id, validation: :cast
pharm_id: {“is invalid”, [type: :id, validation: :cast]}]
Included is the schema for GP (CDoctor and Pharmacy are almost indentical so need no need for all of them) and the patient schema.
Patient schema:
defmodule CmsV1.Patient do
use CmsV1.Web, :model
@primary_key {:id, :binary_id, autogenerate: true}
@derive {Phoenix.Param, key: :id}
schema "patients" do
field :ph_number, :integer
field :name, :binary
field :address, :binary
field :ppsn, :binary
field :date_of_birth, Ecto.Date
field :gender, :string
field :medical_card_present, :boolean, default: false
field :medical_card_number, :binary
field :medical_card_expiry, Ecto.Date
field :mobile_number, :string
field :landline_number, :string
field :nok_name, :string
field :nok_address, :string
field :nok_mobile_number, :string
field :nok_landline_number, :string
field :active, :boolean, default: false
field :active_details, :string
belongs_to :gps, CmsV1.Gp, foreign_key: :gp_id
belongs_to :cdoctors, CmsV1.Cdoctor, foreign_key: :cdoctor_id
belongs_to :pharms, CmsV1.Pharm, foreign_key: :pharm_id
timestamps()
end
@doc """
Builds a changeset based on the `struct` and `params`.
"""
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:ph_number, :name, :address, :ppsn, :date_of_birth, :gender, :medical_card_present, :medical_card_number, :medical_card_expiry, :mobile_number, :landline_number, :nok_name, :nok_address, :nok_mobile_number, :nok_landline_number, :active, :active_details, :gp_id, :cdoctor_id, :pharm_id])
|> validate_required([:ph_number, :name, :address, :ppsn, :date_of_birth, :gender, :medical_card_present, :medical_card_number, :medical_card_expiry, :mobile_number, :landline_number, :nok_name, :nok_address, :nok_mobile_number, :nok_landline_number, :active, :active_details])
|> assoc_constraint(:gps)
|> assoc_constraint(:cdoctors)
|> assoc_constraint(:pharms)
end
end
GP schema:
defmodule CmsV1.GP do
use CmsV1.Web, :model
@primary_key {:id, :binary_id, autogenerate: true}
@derive {Phoenix.Param, key: :id}
schema "gps" do
field :name, :string
field :address, :string
field :mobile_number, :string
field :landline_number, :string
field :fax, :string
field :email, :string
field :training_level, :string
has_many :patients, CmsV1.Patient
timestamps()
end
@doc """
Builds a changeset based on the `struct` and `params`.
"""
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:name, :address, :mobile_number, :landline_number, :fax, :email, :training_level])
|> validate_required([:name, :address, :mobile_number, :landline_number, :fax, :email, :training_level])
end
end
Let me know if anyone can spot anything that may be askew, and thank you!