Question

Assume you have already done a defdb. I have a table "items" which has two fields, "id" (PK) and "item". I want to set up an entity so that whenever I select it, I only get the entries in "item". Judging by the documentation I thought korma/entity-fields was the way to do this.

(require '[korma.core :as korma])

(korma/defentity items
  (korma/entity-fields :item))

(korma/select items)
;; Wanted: [{:item "foo"}]
;; Received: [{:id 1, :item "foo"}]

I'm using korma 0.3.0-beta7. How can I make korma/select do what I want?

Was it helpful?

Solution

I don't think you can do this from the defentity--the way select works without passing a list of fields explicitly is just select *.

How about a helper-macro based on korma/select:

(defmacro select-without-id
  [ent & body]
  `(let [query# (-> (korma/select* ~ent)
                (korma/fields (vec (:fields ~ent)))
                 ~@body)]
     (korma/exec query#)))

OTHER TIPS

id is a defauld PK column name in Korma. That's why you see it in result list. You can remove it by using transform definition:

(korma/defentity items
  (korma/entity-fields :item)
  (korma/transform #(dissoc % :id)))

Try creating a new namespace with the following dependencies:

(ns yourproject.path.kormastuff
    (:use [korma.core]
          [korma.db]))

Then create your entities as such:

(defentity items
  (entity-fields :item))

I think you're probably hitting a wall because you are trying to alias a core and you are missing the db. It's much better to push this stuff into its own file and call the queries you want, with aliases if you chose, from other namespaces, ie:

(ns project.core
    (:use [project.path.kormastuff :as kormadb]))

Then:

(select kormadb/items)

to get your results. That should work.

In the REPL, simply enter

items 

into the command prompt to see the output Korma will give you. There should be no other fields showing up. If you run (select items) in the REPL it will show up everything whether you want that to happen or not. To get the real output you probably have to do a (format "%s" items) in a browser window or something.

If all that doesn't work, there's no shame in using the normal (select db (fields [:item])) clause. You'll find you'll be forced to destructure to use the information anyways.

EDIT:::::::

I'm sorry, I didn't explain myself clearly.

You aren't supposed to do

(select kormadb/items) 

to get what you want. You only need to call the entity itself, so you only need to call items in the REPL, NOT (select items) in the REPL.

You would want to do something like

(def myQuery kormadb/items)

$ myQuery

and you will see that there are no other fields selected by default.

You can also do

(let [myQuery kormadb/items] 
    (format "%s" myQuery))

I'm modifying here, but what I get is similar to this:

{:table "items", :name "items", :pk :id, :db nil, :transforms (), :prepare
s (), :fields ("\"items\".\"item\""), :rel {}}

In order to use myQuery, you need to destructure the above hash-map. I'll leave that as an exercise for you to learn because destructuring is important and hard to learn unless you get your own hands dirty, but I'll give you a little place to start that won't give you an answer, but won't throw an error either:

(let [{myAlias :fields} myQuery]
    (format "%s" myAlias))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top