Skip to content

No such column error on update query with joins and aggregates #254

@Zinggi

Description

@Zinggi

Hi there 👋
Sorry for the long code samples, I haven't had the time to shrink it down to the essentials.
I'll try to get it down to a much smaller example, I'll add that tomorrow, promised 🤞

The code:

    aggregates_query =
      from(a in Album,
        left_join: t in assoc(a, :tracks),
        group_by: a.id,
        select: %{id: a.id, duration: sum(t.duration), n_tracks: count(t), mtime: max(t.mtime)}
      )

    from(a in Album,
      join: s in subquery(aggregates_query),
      on: s.id == a.id,
      update: [set: [duration: s.duration, n_tracks: s.n_tracks, mtime: s.mtime]]
    )
    |> Repo.update_all([])

Full error:

     ** (Exqlite.Error) no such column: st1
     UPDATE "albums" AS a0 SET "duration" = s1."duration", "n_tracks" = s1."n_tracks", "mtime" = s1."mtime" FROM (SELECT sa0."id" AS "id", sum(st1."duration") AS "duration", count(st1) AS "n_tracks", max(st1."mtime") AS "mtime" FROM "albums" AS sa0 LEFT OUTER JOIN "tracks" AS st1 ON st1."album_id" = sa0."id" GROUP BY sa0."id") AS s1 WHERE (s1."id" = a0."id")

Schemas:

  schema "albums" do
    field(:name, :string)
    field(:thumbnail, :string)
    field(:date, :string)

    field(:duration, :float)
    field(:n_tracks, :integer)
    field(:mtime, :naive_datetime)

    belongs_to(:artist, Artist)
    has_many(:tracks, Track)
  end

  schema "tracks" do
    field(:title, :string)
    field(:url, :string)
    field(:thumbnail, :string)
    field(:date, :string)
    field(:duration, :float)
    field(:genre, :string)
    field(:track, :integer)
    field(:disc, :integer)
    field(:mtime, :naive_datetime)
    field(:duplicates, {:array, :string})

    belongs_to(:artist, Artist)
    belongs_to(:album, Album)
  end

The exact same query works with postgres.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions