korma / Korma

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

Is fields macro useless? #340

Closed paomian closed 6 years ago

paomian commented 8 years ago
(defentity user
    (pk :id)
    (entity-fields :a :b :c))
(sql-only (select user (fields :a)))
;;=> "SELECT `user`.`a`, `user`.`b`, `user`.`c` FROM `user`"
immoh commented 8 years ago

It's not, it adds fields on top of fields defined in the entity:

(sql-only (select user (fields :a)))
;;=> "SELECT \"user\".\"a\", \"user\".\"b\", \"user\".\"c\", \"user\".\"a\" FROM \"user\""
paomian commented 8 years ago

I means how i change the default field which be select. for example:

(select user (fields :a))

just select user.a from user not add default field such as b c.

SeriousBug commented 8 years ago

@paomian My solution to the same problem was creating a function like this:

(defn limit-fields
  [ent & fields]
  (assoc ent :fields fields))

Then, you can use this in your queries to override the fields.

(select user
  (limit-fields :a)) ; Will now only select user.a
immoh commented 8 years ago

Or just don't define them as entity-fields if you don't want to always include them. This has been also discussed in #251.

jdpopkin commented 8 years ago

I started using Korma earlier today and I ran into the same misunderstanding. Having read the other issue, I like the fact that Korma's query manipulation functions are additive-only, but I think some extra documentation might be helpful. Specifically, I'd like it if the documentation for select queries mentioned that fields specified in fields are selected in addition to those specified in entity-fields.

immoh commented 8 years ago

I agree that the documentation can be improved. I'll be happy to take a pull request improving docstrings and sqlkorma.com documentation.

venantius commented 6 years ago

This issue seems to have been addressed and so I am going to close it.