Entities and namespaces

98 views
Skip to first unread message

Mike Aldred

unread,
Apr 19, 2012, 1:28:41 AM4/19/12
to sqlk...@googlegroups.com
G'day,

I've been switching my existing code over to Korma, and I've been creating a namespace for each of my entities, so I can try and group them logically together.
The only thing I'm having trouble with, is that when you define a relationship, it ends up resolving the sub entity to the current namespace:

I believe it's in the rel function in core.clj:

(defn rel
  [ent sub-ent type opts]
  (let [var-name (-> sub-ent meta :name)
        cur-ns *ns*]
    (assoc-in ent [:rel (name var-name)]
              (delay
                (let [resolved (ns-resolve cur-ns var-name)
                      sub-ent (when resolved
                                (deref sub-ent))]
                  (when-not (map? sub-ent)
                    (throw (Exception. (format "Entity used in relationship does not exist: %s" (name var-name)))))
                  (create-relation ent sub-ent type opts))))))

What is the reason for this?

I'm currently preferring to have an entity defined like:

(ns database.iso.isos
  (require (database.iso [customers :as customers]))
  (use korma.core))

(defentity iso
  (pk :ixIso)
  (table :Iso)
  (belongs-to customers/customer {:fk :ixCustomer})
  (database connection/production))

Which won't work, Clojure is fairly new to me, and I'm not that good at programming in general, so I was wondering if there was a better way for me to layout my entities?

John Cromartie

unread,
Apr 19, 2012, 7:55:06 AM4/19/12
to sqlk...@googlegroups.com
It doesn't always make sense to put related entities in separate
namespaces, because you can easily end up with namespace reference
cycles (i.e. two namespaces depend on each other, which makes each
impossible to load).

This situation comes up frequently with data modeling. This might be
why Korma does this.

Reply all
Reply to author
Forward
0 new messages