korma / Korma

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

using with when no default database causes spurious error #235

Closed hlship closed 9 years ago

hlship commented 10 years ago

I get a very odd error when using with in a nested query, when there is no default database.

broken code:

(defn- add-roles
  [auth-db user]
  (assoc user :roles
              (->> (k/select e/roles
                             (k/database auth-db)
                             (k/fields :id :uuid :name :created_at :updated_at)
                             (k/join e/users)
                             (k/where {:users.id (user :pk)})
                             (k/with e/permissions
                                     (k/fields :name)))
                   (map flatten-permissions-to-names)
                   (map #(set/rename-keys % {:uuid :id})))))

error:

  1) flashiz.resources.users show can include the list of roles
     db-spec {:options {:delimiters "", :naming {:keys #<string$lower_case clojure.string$lower_case@4f0504f3>, :fields #<core$identity clojure.core$identity@645f9828>}, :alias-delimiter " AS "}} is missing a required parameter
     java.lang.IllegalArgumentException: db-spec {:options {:delimiters "", :naming {:keys #<string$lower_case clojure.string$lower_case@4f0504f3>, :fields #<core$identity clojure.core$identity@645f9828>}, :alias-delimiter " AS "}} is missing a required parameter
       clojure.java.jdbc.deprecated/get-connection  deprecated.clj:  197
     clojure.java.jdbc.deprecated/with-connection*  deprecated.clj:  305
                                 korma.db/do-query          db.clj:  259
                                   korma.core/exec        core.clj:  482
                   korma.core/with-many-to-many/fn        core.clj:  751
                               clojure.core/map/fn        core.clj: 2557
                                               ...                      
                                  clojure.core/seq        core.clj:  133
                               clojure.core/map/fn        core.clj: 2551
                                               ...                      
                                  clojure.core/seq        core.clj:  133
                               clojure.core/map/fn        core.clj: 2551
                                               ...                      
                                clojure.core/first        core.clj:   55
                     users-spec/eval26446/fn/fn/fn  users_spec.clj:  149

fixed code:

(defn- add-roles
  [auth-db user]
  (assoc user :roles
              (->> (k/select e/roles
                             (k/database auth-db)
                             (k/fields :id :uuid :name :created_at :updated_at)
                             (k/join e/users)
                             (k/where {:users.id (user :pk)})
                             (k/with e/permissions
                                     (k/database auth-db) ; <----
                                     (k/fields :name)))
                   (map flatten-permissions-to-names)
                   (map #(set/rename-keys % {:uuid :id})))))

Obviously, with when nested this way should keep using the same database connection as the outer query (what else would make sense?) and perhaps its trying and failing, but the error is very, very odd and hard to track down.

immoh commented 10 years ago

Unfortunately I managed make this a bit worse, so now it throws NPE in 0.3.2. I've fixed this in master so that you get the standard "No valid DB connection selected." error instead. I will also look into properly fixing this so that the connection of the outer query would be automatically used.

immoh commented 9 years ago

Fixed in 0.3.3. Db connection from outer query is now used.