korma / Korma

Tasty SQL for Clojure.
http://sqlkorma.com
1.48k stars 222 forks source link

Subselect breaks after uberjar using composed queries #401

Open Skiggz opened 5 years ago

Skiggz commented 5 years ago

I ran into issues where normal select functions were getting long (method too long!) so I started splitting them up to use predefined base queries (i.e. select*).

However, subselect gets messed up after uberjar (probably some issue with AOT or macro expansion but I couldn't dig enough to figure it out). I did find out what the issue is however, just not the cause. The SQL being generated changes from being wrapped in ticks to being wrapped in quotes ".

Long story short, this works:

(select :foo
        (fields
          [:foo.bar :title]
          [(subselect :bazz
                      (aggregate (avg :bazz.score) :avg_score)
                      (where (= :bazz.foo_id :foo.id))
                      (order :bazz.id :DESC)
                      (limit 10)) :rating])
        (where {:foo.age 10}))

This doesn't work:

;; create a base query to re use 
(def base (-> (select* :foo)
              (fields
                [:foo.bar :title]
                [(subselect :bazz
                            (aggregate (avg :bazz.score) :avg_score)
                            (where (= :bazz.foo_id :foo.id))
                            (order :bazz.id :DESC)
                            (limit 10)) :rating])))

;; query off the base query - works fine, until you uberjar, then subselect gets messed up
(select base
        (where {:foo.age 10}))

It's important to note, the latter does work during lein run but does not work after uberjar-ing and running the jar.

The SQL that gets messed up is in the subselect when broken is like so:

... (SELECT AVG("bazz"."score") AS "avg_score" ...

Which during lein run looks like this, and works as expected

... (SELECT AVG(`bazz`.`score`) AS `avg_score` ...

You'll notice the ticks replaced with quotes in the working and non working post jar. This is using version 0.4.3.