工作模式默認belongs_to的關聯值
schema "jobs" do
belongs_to :status, Test.JobStatus,
foreign_key: :status_id,
references: :id,
type: :string
timestamps()
end
,我有一個狀態型號爲:
@primary_key {:id, :string, autogenerate: false}
schema "job_statuses" do
field :title, :string
field :description, :string
end
當我將這份工作,我需要把默認的工作狀態(如果它不是在PARAMS )。我知道belongs_to關聯中的默認值,但這可能是在您分配關係時分配默認值。任何人都可以指出我可以如何爲任何新創建的作業設置默認狀態(假設作業狀態ID爲「acitve」且其已存在於數據庫中)。樣品已經在這裏https://github.com/tanweerdev/jobs
克隆項目後,只是這樣做
Interactive Elixir (1.5.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> Test.Jobs.create_job_status()
iex(2)> Test.Jobs.test_default_status()
(Postgrex.Error)ERROR 23502(not_null_violation):在 列 「STATUS_ID」 空值違反非空限制
注意:如果我在遷移中添加默認值,但插入和狀態成功關聯時沒有錯誤,但是我返回給用戶的changeset對象不知道該更改,並且它返回null'status_id' –