korma / Korma

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

Foreign table join not used if transform present #363

Open rukor opened 7 years ago

rukor commented 7 years ago

If i have a transform in a foreign table then it doesnt include it in the joins for the query:

Lets say I have:

(defn update? [m k func]
  (if (m k)
    (update-in m [k] func)
    m))

(defentity table1
  (transform #(-> % (update? :description clob-to-string))))

(defentity table2
  (belongs-to table1 {:fk :table1-id}))

I get this:

(-> (k/select* d/table2)
    (k/fields :id)
    (k/with d/table1
            (k/fields :field2))
    (k/as-sql))
=> "SELECT \"table2\".\"id\" FROM \"table2\""

Whereas, if I remove the transform, I get

(-> (k/select* d/table2)
    (k/fields :id)
    (k/with d/table1
            (k/fields :field2))
    (k/as-sql))
=>
"SELECT \"table2\".\"id\", \"table1\".\"field2\" FROM \"table2\" LEFT JOIN \"table1\" ON \"table1\".\"id\" = \"table2\".\"table1-id\""
immoh commented 7 years ago

In order to be able to run the transform, two queries are executed:

(dry-run 
  (select table2 
          (fields :id)
          (with table1
                (fields :field2))))
dry run :: SELECT "table2"."id" FROM "table2" :: []
dry run :: SELECT "table1"."field2" FROM "table1" WHERE ("table1"."id" = ?) :: [1]
=> ({:id_2 1, :table1-id 1, :id 1})
venantius commented 6 years ago

I'm going through old issues and trying to clean them up. Has this issue persisted?

If there hasn't been a response to this issue in 2 weeks, I'll close the ticket.