-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