LauJensen / clojureql

ClojureQL is superior SQL integration for Clojure
https://clojureql.sabrecms.com
Eclipse Public License 1.0
285 stars 39 forks source link

Qualify WHERE clause references when merging subselects #60

Closed bendlas closed 13 years ago

bendlas commented 13 years ago

https://gist.github.com/772953

In this example two table defs that work fine by themselves, produce an ambigous column ref error when joined. This is because both have a column :table_schema and the first table has a WHERE clause on it.

When the join is flattened into a single SQL select, the column ref in the WHERE clause is not table-qualified, leading to the error.

LauJensen commented 13 years ago
(ns gist
  (:require [clojureql.core :as cql]))

(def schema-tables
  (cql/select
   (cql/table *db* :information_schema.tables)
   (cql/where (= :table_schema "public"))))

(def table-columns
  (cql/project
   (cql/table server/*db* :information_schema.columns)
   [:data_type]))

(def table-desc
  (cql/join schema-tables table-columns :table_name))

@table-desc
;; throws (PG), since column :table_schema is ambigous
;; current workaround is to write the WHERE clause as (= :tables.table_schema "public")

Pasting here for ease of reading

LauJensen commented 13 years ago

Also including repl output (bendlas: please always add this to issue reports)

gist> table-desc
SELECT information_schema.tables.*,information_schema.columns.data_type FROM information_schema.tables JOIN information_schema.columns USING(table_name) WHERE (table_schema = public)
LauJensen commented 13 years ago

This commit should fix your problem https://github.com/LauJensen/clojureql/commit/bf9f5c2ed42dc62937ff8ea008cc7d1adb5a77e1