0
tag.parents
및 tag.children
이 각각 다른 태그 목록을 반환하도록 parent 및 children 태그가있는 태그를 사용하고 싶습니다.Ecto에서 heirarchical 태그 사용 방법
내 쿼리를 작성하지 않고도이 작업을 수행 할 수 있습니까?
tag.parents
및 tag.children
이 각각 다른 태그 목록을 반환하도록 parent 및 children 태그가있는 태그를 사용하고 싶습니다.Ecto에서 heirarchical 태그 사용 방법
내 쿼리를 작성하지 않고도이 작업을 수행 할 수 있습니까?
나는 그것을 할 수 있었다. 내가 먼저 tag_relationship 테이블을 생성 :
create table(:tag_relationship, primary_key: false) do
add :parent_id, references(:tags)
add :child_id, references(:tags)
end
스키마에게 그것을 & 변경 집합
defmodule Notebook.TagRelationship do
use Notebook.Web, :model
@primary_key false
schema "tag_relationship" do
belongs_to :parent, Tag
belongs_to :child, Tag
end
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:parent_id, :child_id])
|> validate_required([:parent_id, :child_id])
end
end
이제 설정을 지정 내가 명시 적으로 키 이름을 설정
schema "tags" do
field :name, :string
many_to_many :parents, Notebook.Tag, [join_through: Notebook.TagRelationship,
join_keys: [child_id: :id, parent_id: :id]]
many_to_many :children, Notebook.Tag, [join_through: Notebook.TagRelationship,
join_keys: [parent_id: :id, child_id: :id]]
timestamps()
end
모든 태그 모델 내의 many_to_many 관계 나는 그것이 무엇을 추론 하려는지 알지 못했기 때문에.
지금은
b를의 TagRelationship의 변경 집합 태그 A의c = TagRelationships.changeset(%TagRelationship{},
{parent_id: a.id, child_id: b.id})
Repo.insert(c)
과의 관계를 만들 수 있습니다