clj-record Library: Handling Namespaces

8 views
Skip to first unread message

Stefan Rohlfing

unread,
Oct 20, 2010, 4:09:47 AM10/20/10
to Clojure
Dear Clojure group,

I am currently reading chapter 9.1 MySQL & clj-record of 'Clojure in
Action'.

clj-record seems pretty awesome, but I once again got lost in
namespace jungle.

Here is a concrete example:

--------------------------------------------------------------------------------

File 1: src/active-record/credentials.clj
[Database credentials]

(ns active-record.credentials)

(def db
{:classname "com.mysql.jdbc.Driver"
:subprotocol "mysql"
:user "xxxx"
:password "xxxx"
:subname "//localhost:3306/damages_dev"
})


--------------------------------------------------------------------------------

File 2: src/active-record/user.clj
[Belongs to the database name with the name 'users']


(ns active-record.user
(:use [active-record.credentials :only [db]])
(:require clj-record.boot))


(clj-record.core/init-model
(:associations
(has-many charges)))


--------------------------------------------------------------------------------


File 3: src/active-record/charge.clj
[Belongs to the database name with the name 'charges']


(ns active-record.user
(:use [active-record.credentials :only [db]])
(:require clj-record.boot))


(clj-record.core/init-model
(:associations
(has-many charges)))


--------------------------------------------------------------------------------

I want to add/remove/alter table entries in a different file:

File 4: src/active-record/program/core.clj

All my efforts to import the user.clj and charge.clj namespaces have
failed so far. For example, I have tried the following namespace
declaration:

(ns active-record.program.core
(:require [active-record.user :as user])
(:require [active-record.charge :as charge])
(:require clj-record.boot))


(user/create
{:login "rob"
:first_name "Robert"
:last_name "Berger"
:password "secret"
:email_address "r...@runa.com"})

;; No such var: user/create
;; [Thrown class java.lang.Exception]



This one doesn't work either:

(ns active-record.program.core
(:require [active-record.user :as user])
(:require [active-record.charge :as charge]))

;; EOF while reading
;; [Thrown class java.lang.Exception]



Can anybody tell me what went wrong in the example above?


Stefan

Michael Wood

unread,
Oct 20, 2010, 4:20:18 AM10/20/10
to clo...@googlegroups.com

Just a guess, but try something other than "user". The user namespace
is the default namespace when using the repl and maybe there's some
sort of conflict there.

I'm not sure how likely that is, but it's worth a try, I think :)

>   (:require [active-record.charge :as charge])
>     (:require clj-record.boot))
>
> (user/create
>  {:login "rob"
> :first_name "Robert"
> :last_name "Berger"
> :password "secret"
> :email_address "r...@runa.com"})
>
> ;; No such var: user/create
> ;;  [Thrown class java.lang.Exception]
>
> This one doesn't work either:
>
>  (ns active-record.program.core
>   (:require [active-record.user :as user])
>   (:require [active-record.charge :as charge]))
>
> ;; EOF while reading
> ;;  [Thrown class java.lang.Exception]
>
> Can anybody tell me what went wrong in the example above?

--
Michael Wood <esio...@gmail.com>

Stefan Rohlfing

unread,
Oct 20, 2010, 4:33:40 AM10/20/10
to Clojure
The name 'user' is taken from an example in the book. The author
executes the code at the REPL like this:

(require '(active-record [user :as user]))

user=> (user/create {:login "rob"
:first_name "Robert"
:last_name "Berger"
:password "secret"
:email_address "r...@runa.com"})

And then there is the clj-record macro 'init-model' that creates a
model out of a Clojure namespace. However, I don't quite understand
how this affects bringing in the namespaces.
Here is the code:
http://github.com/duelinmarkers/clj-record/blob/master/src/clj_record/core.clj
(at the bottom)



On Oct 20, 4:20 pm, Michael Wood <esiot...@gmail.com> wrote:
> Michael Wood <esiot...@gmail.com>

Meikel Brandmeyer

unread,
Oct 20, 2010, 4:37:13 AM10/20/10
to Clojure
Hi,

On 20 Okt., 10:09, Stefan Rohlfing <stefan.rohlf...@gmail.com> wrote:

> ;; EOF while reading
> ;;  [Thrown class java.lang.Exception]

Are you sure, that you don't have some syntax error somewhere?

Sincerely
Meikel

Stefan Rohlfing

unread,
Oct 20, 2010, 4:46:05 AM10/20/10
to Clojure
You are right, there was a syntax error in charge.clj. However, after
correcting the error I get the same error message as with the other
namespace declaration:

;; No such var: user/create
;; [Thrown class java.lang.Exception]

Meikel Brandmeyer

unread,
Oct 20, 2010, 5:03:25 AM10/20/10
to Clojure
Hi,

On 20 Okt., 10:46, Stefan Rohlfing <stefan.rohlf...@gmail.com> wrote:

> You are right, there was a syntax error in charge.clj. However, after
> correcting the error I get the same error message as with the other
> namespace declaration:

Did you try the same with charge/create? Examples in books are not
necessarily correct.

Sincerely
Meikel

Stefan Rohlfing

unread,
Oct 20, 2010, 5:09:05 AM10/20/10
to Clojure
Yes, I also tried charge/create. The error message is different, but
it still does not work:

;; No such namespace: charge
;; [Thrown class java.lang.Exception]

Meikel Brandmeyer

unread,
Oct 20, 2010, 5:23:29 AM10/20/10
to Clojure
Hi,

On 20 Okt., 11:09, Stefan Rohlfing <stefan.rohlf...@gmail.com> wrote:

> Yes, I also tried charge/create. The error message is different, but
> it still does not work:

More evidence for a problem with the user alias. Try a different one
like (:require [active-record.user :as u]).

Sincerely
Meikel

Stefan Rohlfing

unread,
Oct 20, 2010, 5:35:44 AM10/20/10
to Clojure
Evaluating your suggested declaration:

(ns active-record.program.core
(:require [active-record.user :as u])
(:require [active-record.charge :as charge]))

I get the following error message:

;; Unable to resolve symbol: user=> in this context
;; [Thrown class java.lang.Exception]

The same happens if I do not include the active-record.user namespace:

(ns active-record.program.core
(:require [active-record.charge :as charge]))


;; Unable to resolve symbol: user=> in this context
;; [Thrown class java.lang.Exception]

Meikel Brandmeyer

unread,
Oct 20, 2010, 5:45:19 AM10/20/10
to Clojure
Hi,

On 20 Okt., 11:35, Stefan Rohlfing <stefan.rohlf...@gmail.com> wrote:

> Evaluating your suggested declaration:
>
>  (ns active-record.program.core
>    (:require [active-record.user :as u])
>    (:require [active-record.charge :as charge]))
>
> I get the following error message:
>
> ;; Unable to resolve symbol: user=> in this context
> ;;   [Thrown class java.lang.Exception]

You copy'n'pasted a clojure prompt somewhere or your repl is messed
up. Please check your files thoroughly and (if necessary) start with a
fresh repl.

Sincerely
Meikel

Stefan Rohlfing

unread,
Oct 20, 2010, 8:04:09 AM10/20/10
to Clojure
You were right again. I left a code snippet from the book in
charge.clj and forgot to remove it later.

Now everything seems to work fine, even when importing the active-
record.user namespace as 'user':

----------------------------------------------------------------
(ns active-record.program.core
(:require [active-record.user :as user])
(:require [active-record.charge :as charge]))

(user/create
{:login "my-login"
:first_name "Stefan"
:last_name "Rohl"
:password "secret"
:email_address "ste...@example.com"})

;; {:email_address "ste...@example.com", :password "secret",
;; :last_name "Rohl", :first_name "Stefan", :login "my-login",
;; :id 8}

(charge/create
{:user_id 8,
:amount_dollars 27
:amount_cents 91
:category "meals"
:vendor_name "Metro"
:date "2010-01-15"})

;; {:date "2010-01-15", :vendor_name "Metro", :category "meals",
;; :amount_cents 91, :amount_dollars 27, :id 7,
;; :user_id 8}

----------------------------------------------------------------

Thank you so much for your help! I really learned at lot about dealing
with namespaces today.

Stefan

Meikel Brandmeyer

unread,
Oct 20, 2010, 8:34:39 AM10/20/10
to Clojure
Hi,

On 20 Okt., 14:04, Stefan Rohlfing <stefan.rohlf...@gmail.com> wrote:

> I really learned at lot about dealing with namespaces today.

I hope you also learned a bit about error messages. ;)

"No such var: user/create": That means you get past the namespace
declaration. Hence they load fine. But in the user namespace the model-
init call was obviously not done. => Possible questions: maybe the
file was not saved? maybe there is a spurious .class file around
messing up things from a previous AOT compilation run?

"EOF while reading": Now something changed. Either in one of
the :required namespaces was a syntax error introduced, or the init-
model now happens and the syntax error is in a-r.program.core after
the user/create call.

I remembered darkly that I had once trouble with Vars which had the
same name as an alias. Hence my questions: "Did you try the same with
charge instead of user?" => Different alias. Does it work? Then the
problem could be the alias.

"No such namespace: charge": So the alias is not done. That means that
the error is either in user, during its alias creation or charge
itself. Just to be sure my question: "Does it work with a different
alias instead of user?"

"Unable to resolve symbol: user=> in this context": again something
changed in the files between our conversation. So we are targeting a
moving target. Hence my request to really check the files and - if in
doubt - use a fresh repl to get a clear foundattion.

There were several occasions in the above description, where I'm
expected an exception, but you didn't report one, and where I'm not
sure whether this was due to the repl interaction or other side
effects (eg. unsaved files, etc.). Also your positive report in the
end leaves the question why it actually works now. From the code you
pasted it should have worked from the beginning.

Anyway, it works. :)

Sincerely
Meikel

Stefan Rohlfing

unread,
Oct 20, 2010, 9:03:06 AM10/20/10
to Clojure
I apologize for presenting you with a moving target. That was
definitely not my attention.

I should have realized I messed up the content of the files but
unfortunately had no clue what all these exceptions meant.

Therefore I really appreciate your detailed description of the
conclusion you draw from the different error messages.

Thanks again for your great help!

Stefan
Reply all
Reply to author
Forward
0 new messages