Search code examples
rubysequel

How to select schema/table/column in Sequel using the shortcut syntax?


In Sequel, one can use :a__b to generate SQL such as "a"."b". This syntax doesn't work though when we want a fully qualified statement to get to a column, such as:

> Sequel.version
=> "4.25.0"
> puts RDB[:store__albums].select(:store__albums__title___album_title).sql
SELECT "store"."albums__title" AS "album_title" FROM "store"."albums"
# I expected:
# SELECT "store"."albums"."title" AS "album_title" FROM "albums"."title"

I know I can use Sequel.lit to generate the correct value, but I expected the fluent syntax to support this. Does Sequel support this out of the box?

I am using the PostgreSQL driver.


Solution

  • According to Jeremy Evans, Sequel does not support the fluent syntax to 3 levels deep.

    Instead, the solution is to use lower-level methods to do so:

    puts RDB[:store__albums].
      select(Sequel.qualify(:store, :albums__title).
               as(:album_title)).sql