sean

sean

PostgreSQL does not support selecting all fields from without a schema

TL;DR The problem I’m trying to solve is analogous to building an Ecto.Query, say, for posts that includes n number of latest associated comments for each.

I realize this has been discussed several times already, but I still have not found a clear solution:

As an example, given the following:

defmodule Comment do
  schema "comments" do
    belongs_to(:post, Post)
  end
end

defmodule Post do
  schema "posts" do
    has_many(:comments, Comment)    
  end

  defmacrop latest_comments(post_id, limit \\ 5) do
    quote do
      fragment(
        "select * from comments
        where comments.post_id = ?
        order by comments.inserted_at desc nulls last
        limit ?",
        unquote(post_id),
        unquote(limit)
      )
    end
  end

  def with_latest_comments(queryable) do
    queryable
    |> join(:left_lateral, [p], c in latest_comments(p.id))
    |> select_merge([p, c], %{comments: c})
  end
end

I think this is so close to a solution, but it raises this error:

PostgreSQL does not support selecting all fields from without a schema. Please specify a schema or specify exactly which fields you want to select in query

If I wrap c with map or struct e.g.:

|> select_merge([p, c], %{comments: struct(c, [:id, :post_id])})

The following error is raised:

it is not possible to return a map/struct subset of a fragment, you must explicitly return the desired individual fields in query

I see this was discussed here #1592 Ecto.QueryError) PostgreSQL requires a schema module when using selector “u0” but none was given.

The following suggestions were mentioned, either of which I think would work great!

We could also support struct(u, [:foo, :bar, :baz], MyModule)

or even struct(u, MyModule) if there are all the columns since the module contains the column names

But alas, that issue is from 2016 and has long been closed. :confused:

Any other ideas? Am I missing something obvious?

Thanks!

Most Liked

peerreynders

peerreynders

Correction: Caveat

The issue is that the conditions apply to all the records returned by that single preload query - not just each single association.


preload query.

Example:

iex(1)> alias MusicDB.{Repo,Album,Track}
[MusicDB.Repo, MusicDB.Album, MusicDB.Track]
iex(2)> import Ecto.Query
Ecto.Query
iex(3)> album_id = 2
2
iex(4)> tracks_query = from(t in Track, [
...(4)>    order_by: fragment("? DESC NULLS LAST", t.inserted_at),
...(4)>    limit: 3
...(4)> ])
#Ecto.Query<from t in MusicDB.Track,
 order_by: [asc: fragment("? DESC NULLS LAST", t.inserted_at)], limit: 3>
iex(5)> query = from(a in Album, [
...(5)>   preload: [tracks: ^tracks_query],
...(5)>   where: a.id == ^album_id
...(5)> ])
#Ecto.Query<from a in MusicDB.Album, where: a.id == ^2,
 preload: [tracks: #Ecto.Query<from t in MusicDB.Track, order_by: [asc: fragment("? DESC NULLS LAST", t.inserted_at)], limit: 3>]>
iex(6)> Repo.all(query)

12:43:52.217 [debug] QUERY OK source="albums" db=2.2ms decode=1.7ms
SELECT a0."id", a0."title", a0."inserted_at", a0."updated_at", a0."artist_id" FROM "albums" AS a0 WHERE (a0."id" = $1) [2]
 
12:43:52.223 [debug] QUERY OK source="tracks" db=2.5ms
SELECT t0."id", t0."title", t0."duration", t0."index", t0."number_of_plays", t0."inserted_at", t0."updated_at", t0."album_id", t0."album_id" FROM "tracks" AS t0 WHERE (t0."album_id" = $1) ORDER BY t0."album_id", t0."inserted_at" DESC NULLS LAST LIMIT 3 [2]
[
  %MusicDB.Album{
    __meta__: #Ecto.Schema.Metadata<:loaded, "albums">,
    artist: #Ecto.Association.NotLoaded<association :artist is not loaded>,
    artist_id: 1,
    genres: #Ecto.Association.NotLoaded<association :genres is not loaded>,
    id: 2,
    inserted_at: ~N[2018-06-16 20:29:41.476625],
    title: "Cookin' At The Plugged Nickel",
    tracks: [
      %MusicDB.Track{
        __meta__: #Ecto.Schema.Metadata<:loaded, "tracks">,
        album: #Ecto.Association.NotLoaded<association :album is not loaded>,
        album_id: 2,
        duration: 1061,
        duration_string: nil,
        id: 10,
        index: 5,
        inserted_at: ~N[2018-06-16 20:29:41.480612],
        number_of_plays: 0,
        title: "No Blues",
        updated_at: ~N[2018-06-16 20:29:41.480618]
      },
      %MusicDB.Track{
        __meta__: #Ecto.Schema.Metadata<:loaded, "tracks">,
        album: #Ecto.Association.NotLoaded<association :album is not loaded>,
        album_id: 2,
        duration: 754,
        duration_string: nil,
        id: 9,
        index: 4,
        inserted_at: ~N[2018-06-16 20:29:41.480045],
        number_of_plays: 0,
        title: "Miles",
        updated_at: ~N[2018-06-16 20:29:41.480051]
      },
      %MusicDB.Track{
        __meta__: #Ecto.Schema.Metadata<:loaded, "tracks">,
        album: #Ecto.Association.NotLoaded<association :album is not loaded>,
        album_id: 2,
        duration: 896,
        duration_string: nil,
        id: 8,
        index: 3,
        inserted_at: ~N[2018-06-16 20:29:41.479460],
        number_of_plays: 0,
        title: "Walkin'",
        updated_at: ~N[2018-06-16 20:29:41.479465]
      }
    ],
    updated_at: ~N[2018-06-16 20:29:41.476631]
  }
]
iex(7)> 

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36654 110
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44532 311
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement