2017-11-27 6 views
-1

사용자 지정 이름을 연결에 설정할 수 있습니까?Ecto association 사용자 지정 이름

예를 들어 : 나는 고전 "사용자 -> 게시물"이 상황 :

create table(:users) do 
    add :first_name, :string 
    add :last_name, :string 

    timestamps() 
end 

create table(:posts) do 
    add :text, :string 
    add :user_id, references(:users, on_delete: :nothing) 

    timestamps() 
end 

및 스키마 :

schema "users" do 
    field :first_name, :string 
    field :last_name, :string 
    has_many :posts, MyProj.Post 

    timestamps() 
end 

schema "posts" do 
    field :text, :string 
    belongs_to :user, MyProj.User 

    timestamps() 
end 

내가 posts에 연관이 user하지만 author하지 호출하려는. 나는 내 스키마를 변경하는 경우 :

schema "posts" do 
    field :text, :string 
    belongs_to :author, MyProj.User, [foreign_key: :user_id] 

    timestamps() 
end 

내가 얻을 오류 : field 'author' in 'select' does not exist in schema MyProj.Post

편집 :

def all_posts _root, _args, _info do 
    posts_query = from p in Post, 
    preload: [:author], 
    select: %{ 
     posted_by: p.author, 
     text: p.text 
    } 

    posts = Repo.all(posts_query) 
    {:ok, posts} 
end 

스택 트레이스 :

나는 오류가 모든 게시물을 조회하려고 얻을 :

[info] Sent 500 in 30ms 
[error] #PID<0.478.0> running MyProj.Endpoint terminated 
Server: localhost:4000 (http) 
Request: POST /graphiql 
** (exit) an exception was raised: 
    ** (Ecto.QueryError) lib/my-proj/resolvers/post_resolver.ex:7: field `author` in `select` does not exist in schema MyProj.Post in query: 

from p in MyProj.Post, 
    select: %{posted_by: p.author, text: p.text}, 
    preload: [:author] 

     (ecto) lib/ecto/repo/queryable.ex:124: Ecto.Repo.Queryable.execute/5 
     (ecto) lib/ecto/repo/queryable.ex:37: Ecto.Repo.Queryable.all/4 
     (my-proj) lib/my-proj/resolvers/post_resolver.ex:17: MyProj.PostResolver.all_posts/3 
     (absinthe) lib/absinthe/resolution.ex:147: Absinthe.Resolution.call/2 
     (absinthe) lib/absinthe/phase/document/execution/resolution.ex:191: Absinthe.Phase.Document.Execution.Resolution.reduce_resolution/1 
     (absinthe) lib/absinthe/phase/document/execution/resolution.ex:161: Absinthe.Phase.Document.Execution.Resolution.do_resolve_field/4 
     (absinthe) lib/absinthe/phase/document/execution/resolution.ex:147: Absinthe.Phase.Document.Execution.Resolution.do_resolve_fields/6 
     (absinthe) lib/absinthe/phase/document/execution/resolution.ex:87: Absinthe.Phase.Document.Execution.Resolution.walk_result/5 
     (absinthe) lib/absinthe/phase/document/execution/resolution.ex:57: Absinthe.Phase.Document.Execution.Resolution.perform_resolution/3 
     (absinthe) lib/absinthe/phase/document/execution/resolution.ex:25: Absinthe.Phase.Document.Execution.Resolution.resolve_current/3 
     (absinthe) lib/absinthe/pipeline.ex:247: Absinthe.Pipeline.run_phase/3 

답변

0

협회의 이름이 아닌 Ecto가 어떻게 쿼리를 해석하는지에 대한 오해에서 비롯된 문제.

쿼리의 개체는 "DB 테이블"행 다음 스키마 구조체의 엔티티,

select: %{ 
    posted_by: p.author, 
    text: p.text 
} 

체외이 존재하지 않습니다 host라는 테이블 열을 찾기 위해 노력하고있다 오히려 때문에. 이 쿼리를 수정하려면 "SQL-ish"방식으로 생각해야합니다.

posts_query = from p in Post, 
join: author in assoc(p, :author), 
select: %{ 
    posted_by: author, 
    text: p.text 
}