metabase / toucan

A classy high-level Clojure library for defining application models and retrieving them from a DB
Eclipse Public License 1.0
570 stars 49 forks source link

Select query does not work with sub-query #93

Open pawit-metabase opened 2 years ago

pawit-metabase commented 2 years ago

@camsaul

Example query:

(db/select [User :first-name :last-name]
            :id [:in {:select [:id] :from [User]}]
            {:order-by [:id]})

Error:

  act-msg: exception in actual: (db/select [User :first-name :last-name] :id [:in {:select [:id], :from [User]}] {:order-by [:id]})
    threw: class org.postgresql.util.PSQLException - ERROR: column "select" does not exist
  Position: 80
           org.postgresql.core.v3.QueryExecutorImpl$receiveErrorResponse (QueryExecutorImpl.java:2497)
           org.postgresql.core.v3.QueryExecutorImpl$processResults (QueryExecutorImpl.java:2233)
           org.postgresql.core.v3.QueryExecutorImpl$execute (QueryExecutorImpl.java:310)
           org.postgresql.jdbc.PgStatement$executeInternal (PgStatement.java:446)
           org.postgresql.jdbc.PgStatement$execute (PgStatement.java:370)
           org.postgresql.jdbc.PgPreparedStatement$executeWithFlags (PgPreparedStatement.java:149)
           org.postgresql.jdbc.PgPreparedStatement$executeQuery (PgPreparedStatement.java:108)
           clojure.java.jdbc$execute_query_with_params$invokeStatic (jdbc.clj:1079)
           on (jdbc.clj:1073)
           clojure.java.jdbc$db_query_with_resultset_STAR_$invokeStatic (jdbc.clj:1102)
           on (jdbc.clj:1082)
           clojure.java.jdbc$query$invokeStatic (jdbc.clj:1171)
           on (jdbc.clj:1133)
           toucan.db$query$invokeStatic (db.clj:308)
           toucan.db$query$doInvoke (db.clj:304)
           toucan.db$simple_select$invokeStatic (db.clj:414)
           on (db.clj:403)
           toucan.db$select$invokeStatic (db.clj:708)
           toucan.db$select$doInvoke (db.clj:702)

This issue was introduced in #87. A variant of this was reported in #88 and fixed in #89.

A workaround is to use an explicit {:where [:in :id {:select [:id] :from [User]}]} instead.