2016-12-13 4 views
0

테스트라는 이름의 다형성 구조가 있는데, 공통적으로 (다른 것들 중에서) 정의 된 팀이 있습니다.elixir/ecto가있는 다형 구조에 assoc 미리로드

defmodule Test do 
    def generate_schema(options, type) do 
    quote do 
     schema "tests" do 
     field :name,        :string 
     field :type,        :string, default: unquote(type) 
     embeds_one :options,      unquote(options), on_replace: :delete 
     many_to_many :teams,      Team, join_through: "tests_teams", on_replace: :delete 
     end 
    end 
    end 

    def get_type_from_module(module) when is_atom(module) do 
    module 
    |> to_string 
    |> String.split(".", trim: true) 
    |> List.last 
    end 

    defmacro __using__(options) do 
    type = __CALLER__.module |> get_type_from_module 
    quote do 
     unquote(generate_schema(options, type)) 
    end 
    end 
end 

많은 테스트가 있습니다. TestA

defmodule Tests.TestA do 
    defmodule Options do 
    embedded_schema do 
     field :number_of_jumps, :integer 
    end 
    end 
    use Test, Options 
end 

문제는 테스트를 위해 팀을 미리로드하는 것입니다. 내가

Repo.get!(Tests.TestA, id) 
|> Repo.preload(:teams) 

을 할 때 나는

... 
SELECT t0."id", ... , s1."id" FROM "teams" AS t0 INNER JOIN "tests" AS s1 ON s1."id" = ANY($1) INNER JOIN "tests_teams" AS s2 ON s2."test_a_id" = s1."id" WHERE (s2."team_id" = t0."id") ORDER BY s1."id" [[2]] 
... 
** (Postgrex.Error) ERROR 42703 (undefined_column): column s2.test_a_id does not exist 
... 

은 '... S2의. "test_a_id"...'해야 얻을 'S2의 .... "test_id을"...' 팀을 어떻게 미리 미리로드 할 수 있습니까?

답변

0

이 솔루션은 다음 그것은 또한 체인지 등 :

many_to_many :teams, Team, join_through: "tests_teams", join_keys: [test_id: :id, team_id: :id], on_replace: :delete 
작동에 명시 적으로 defining keys for many_to_many 것 같다