첫 번째 :이 topic을 찾았지만 작동하지 않습니다.Elixir Ecto 빈 삽입 belongs_to
나는 두 가지 모델
Council(1)<->(n)Department
사이의 관계를하고 난 부서에 관계없이 회의를 삽입 할.
CREATE TABLE public.councils
(
id integer NOT NULL DEFAULT nextval('councils_id_seq'::regclass),
name character varying(255),
description character varying(255),
department_id integer,
inserted_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL,
CONSTRAINT councils_pkey PRIMARY KEY (id),
CONSTRAINT councils_department_id_fkey FOREIGN KEY (department_id)
REFERENCES public.departments (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
문서는 cast/4
가되지 않습니다 있다고하지만 피닉스는 그 자체로 cast/3
& validate_required/3
으로 변경 집합을 만들어이 SQL 스키마
schema "councils" do
field :name, :string
field :description, :string
belongs_to :department, Db2.Department
many_to_many :students, Db2.Student, join_through: Db2.StudentCouncil
many_to_many :periods, Db2.Period, join_through: Db2.StudentCouncil
timestamps()
end
:
나는이 체외 스키마있어 . 하여 기본 phoenix.html 양식 요소를 사용하여 양식 I'am를 들어
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:name, :description, :department_id])
|> validate_required([:name])
|> unique_constraint(:name)
|> assoc_constraint(:department)
end
: 나는 단지 내가 다음과 같은 변경 집합이되도록 department
을 추가했습니다.
[debug] QUERY OK db=1.2ms queue=0.1ms
SELECT u0."id", u0."uid", u0."is_admin", u0."is_staff", u0."password_hash", u0."inserted_at", u0."updated_at" FROM "users" AS u0 WHERE (u0."id" = $1) [1]
[debug] Processing by Db2.CouncilController.create/2
Parameters: %{"_csrf_token" => "KwpdFCZ/bFN9fwkcXSAYLhRCIzgOAAAAXeoEa4+d1MoT7SvzZpgOdg==", "_utf8" => "✓", "council" => %{"department_id" => "", "description" => "", "name" => "test2"}}
Pipelines: [:browser]
[debug] QUERY OK db=1.2ms queue=0.1ms
#Ecto.Changeset<action: :insert, changes: %{description: "", name: "test2"},
errors: [department_id: {"is invalid", [type: :id]}], data: #Db2.Council<>,
valid?: false>
woOt? 고맙습니다. 매력처럼 작동 :) –