2017-11-27 4 views
-1

Ist es möglich, einen benutzerdefinierten Namen für eine Zuordnung festzulegen?Ecto Assoziation benutzerdefinierter Name

Zum Beispiel: Ich habe klassische "Benutzer -> Beiträge" -Situation:

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 

und Schema:

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 

I der Verein in posts will nicht user aber author aufgerufen werden. Wenn ich mein Schema zu ändern:

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

    timestamps() 
end 

ich Fehler: field 'author' in 'select' does not exist in schema MyProj.Post

Edited:

ich die Fehler alle Beiträge zur Abfrage versuchen:

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 

Stapel-Spur :

[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 

Antwort

0

Das Problem nicht im Vereinnamen, sondern in meinem Missverständnis, wie interpretiert Ecto Abfragen.

Da Entitäten in Abfragen sind eher „DB-Tabelle“ Zeilen dann Schema struct Entitäten, in

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

Ecto versucht, eine Spalte der Tabelle zu finden host genannt, die es nicht existiert. Um diese Abfrage zu "reparieren", musste ich darüber mehr auf "SQL-ish" Weise denken:

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