Migrations
@table :jobs
def change do
create table(@table) do
add :status_id, references(:job_statuses, column: "id", type: :string), nil: false
end
end
@table :job_statuses
def change do
create table(@table, primary_key: false) do
add :id, :string, primary_key: true
common [:title, :description]
end
end
JobStatusModel
@primary_key {:id, :string, autogenerate: false}
schema "job_statuses" do
common [:title, :description]
end
JobModel
schema "jobs" do
belongs_to :status, Qber.V1.JobStatusModel, foreign_key: :status_id, references: :id, type: :string
timestamps()
end
Changeset of Job
def changeset(struct, params) do
struct
|> cast(params, @required_fields ++ @optional_fields ++ [:status_id])
end
But when I try to create job with the status id passed from user, I got this error: **** (Postgrex.Error) ERROR 22P02 (invalid_text_representation): invalid input syntax for type json**
and
Token “confirmed” is invalid.
(ecto) lib/ecto/adapters/sql.ex:571: Ecto.Adapters.SQL.struct/7
(ecto) lib/ecto/repo/schema.ex:469: Ecto.Repo.Schema.apply/4
(ecto) lib/ecto/repo/schema.ex:205: anonymous fn/13 in Ecto.Repo.Schema.do_insert/4
(ecto) lib/ecto/repo/schema.ex:691: anonymous fn/3 in Ecto.Repo.Schema.wrap_in_transaction/6
(db_connection) lib/db_connection.ex:1366: DBConnection.transaction_nested/2
(db_connection) lib/db_connection.ex:1226: DBConnection.transaction_meter/3
(db_connection) lib/db_connection.ex:790: DBConnection.transaction/3
(ecto) lib/ecto/multi.ex:422: Ecto.Multi.apply_operation/5
(elixir) lib/enum.ex:1826: Enum.“-reduce/3-lists^foldl/2-0-”/3
(ecto) lib/ecto/multi.ex:412: anonymous fn/5 in Ecto.Multi.apply_operations/5
(ecto) lib/ecto/adapters/sql.ex:620: anonymous fn/3 in Ecto.Adapters.SQL.do_transaction/3
(db_connection) lib/db_connection.ex:1275: DBConnection.transaction_run/4
(db_connection) lib/db_connection.ex:1199: DBConnection.run_begin/3
(db_connection) lib/db_connection.ex:790: DBConnection.transaction/3
(ecto) lib/ecto/repo/queryable.ex:21: Ecto.Repo.Queryable.transaction/4
(qber) web/V1/Job/controller.ex:16: Qber.V1.JobController.create/2
(qber) web/V1/Job/controller.ex:1: Qber.V1.JobController.action/2
(qber) web/V1/Job/controller.ex:1: Qber.V1.JobController.phoenix_controller_pipeline/2
(qber) lib/qber/endpoint.ex:1: Qber.Endpoint.instrument/4
(phoenix) lib/phoenix/router.ex:278: Phoenix.Router.call/1
I have spent quite some time on articles like these :belongs_to + custom primary keys
and How to set a belongs_to association
and Ecto schema on git repo
and relationship-in-ecto
and Problems with belongs_to association with custom primary keys
and similar articles. All other similar associations are being cast and inserted perfectly, only this one having customer key is throwing exception. please help me solve this issue