korma / Korma

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

naming function is not applied on joins when using "create-db" instead of "defdb" #280

Closed exi closed 9 years ago

exi commented 9 years ago

I have this configuration:

(ns example.core
  (:gen-class)
  (:use korma.db)
  (:use korma.core)
  (:require [clojure.string :as str]))

(def db-spec (h2 {:db "resources/db/korma.db"
             :user "sa"
             :password ""
             :naming {:keys str/lower-case
                      ;; set map keys to lower
                      :fields str/upper-case}}))

(def db (create-db db-spec))
(declare messages)
(defentity users
  (pk :id)
  (table :USERS)
  (database db)
  (has-many messages)
  (entity-fields :id :name :last_seen))
(defentity messages
  (pk :id)
  (table :MESSAGES)
  (database db)
  (entity-fields :id :message :timestamp)
  (belongs-to users))

(defn -main
  "I don't do a whole lot ... yet."
  [& args]
  (println (sql-only (select messages (with users)))))

When joining entities with (with) the naming functions is not applied on the joined entity, leading to incorrect lower case column names in my example:

> (select messages (with users))
Failure to execute query with SQL:
SELECT "MESSAGES"."ID", "MESSAGES"."MESSAGE", "MESSAGES"."TIMESTAMP", "USERS"."id", "USERS"."name", "USERS"."last_seen" FROM "MESSAGES" LEFT JOIN "USERS" ON "USERS"."id" = "MESSAGES"."USERS_id"  ::  []
JdbcSQLException:
 Message: Column "USERS.id" not found; SQL statement:
SELECT "MESSAGES"."ID", "MESSAGES"."MESSAGE", "MESSAGES"."TIMESTAMP", "USERS"."id", "USERS"."name", "USERS"."last_seen" FROM "MESSAGES" LEFT JOIN "USERS" ON "USERS"."id" = "MESSAGES"."USERS_id" [42122-170]
 SQLState: 42S22
 Error Code: 42122

JdbcSQLException Column "USERS.id" not found; SQL statement:
SELECT "MESSAGES"."ID", "MESSAGES"."MESSAGE", "MESSAGES"."TIMESTAMP", "USERS"."id", "USERS"."name", "USERS"."last_seen" FROM "MESSAGES" LEFT JOIN "USERS" ON "USERS"."id" = "MESSAGES"."USERS_id" [42122-170]  org.h2.message.DbException.getJdbcSQLException (DbException.java:329)
immoh commented 9 years ago

You need to use defdb:

(defdb db (h2 {...}))
exi commented 9 years ago

I was using defdb, i just forgot to paste that line of code. I edited it for clarity. I just realized that this issue is the same that is addressed here: https://github.com/korma/Korma/pull/268

ls4f commented 9 years ago

Actually, I have been meaning to open an issue, but I never seem to have the time for a pull request and it seems relevant here. defdb sets the default naming, which is great but with-db doesn't honor what is set for the specific database. I figure it's not a huge issue since I use the same naming on all my databases, but it seems like a bug to me. If nothing else - it have no other reason to use defdb.

immoh commented 9 years ago

@exi: If you think this is the same as #268, have you omitted the entity transforms also "for clarity"? I still cannot reproduce this with your code example.

@ls4f: Looking at the code, that seems to be the case with with-db. Pull request welcome :)

exi commented 9 years ago

@immoh you are absolutely right. I already deleted my code when i replied earlier. I just realized that i didn't use "defdb" but "create-db".

My apologies for the pointless confusion. A complete example project can be found here: https://github.com/exi/korma-example

Just executing "lein run" will print:

SELECT "MESSAGES"."ID", "MESSAGES"."MESSAGE", "MESSAGES"."TIMESTAMP", "USERS"."id", "USERS"."name", "USERS"."last_seen" FROM "MESSAGES" LEFT JOIN "USERS" ON "USERS"."id" = "MESSAGES"."USERS_id"

I will update the issue title and initial description to properly reflect the error. The expected behavior would be that the naming function is properly applied when using "create-db" as well as "defdb".

immoh commented 9 years ago

@exi You're using old version of Korma. It works correctly with 0.4.0.

exi commented 9 years ago

@immoh Thanks for the response. Could you please redeploy "http://sqlkorma.com/docs" from the current repository version? The website is still stating:

;; First you'll need to add Korma as a dependency in your lein/cake project:
[korma "0.3.0"]
immoh commented 9 years ago

Oh crap, it still has old version. Unfortunately I don't have access to do it.

@ibdknox: any chance to get sqlkorma.com updated?

immoh commented 9 years ago

sqlkorma.com is now up-to-date.

I created separate issue for bug reported by @ls4f: #282 so that I can close this.