New feature request/implementation: override fk/model name

1 view
Skip to first unread message

Jim Menard

unread,
Jun 13, 2010, 6:38:59 PM6/13/10
to clj-rec...@googlegroups.com
In my fork of clj-record (http://github.com/jimm/clj-record), I've
implemented the ability to override the foreign key and model names
for belongs-to and has-many associations. There are docs and tests as
well.

I've sent a pull request to John.

Jim
--
Jim Menard, http://www.io.com/~jimm/

John D. Hume

unread,
Jun 14, 2010, 7:47:17 AM6/14/10
to clj-rec...@googlegroups.com
Just pushed this. Thanks for the contribution. Will try to pull Matt
Courtney's lein stuff tonight.

--
http://elhumidor.blogspot.com/

Jim Menard

unread,
Jun 14, 2010, 9:37:30 AM6/14/10
to clj-rec...@googlegroups.com
John,

Thank you for pulling my changes. I have a question about the
simplification you did in associations/belongs-to. I had

I had

foreign-key-attribute (keyword (cond
(:fk opts) (:fk opts)
(:model opts) (str
(dashes-to-underscores (str association-name)) "_id")
:else (str (dashes-to-underscores
associated-model-name) "_id")))

which you changed to

foreign-key-attribute (keyword (or
(:fk opts)
(str (dashes-to-underscores
associated-model-name) "_id")))

However I don't think this will work as intended (at least, as I
intended). Given the association

(belongs-to mother :model person)

the code I submitted will infer a foreign key name of mother_id, which
is the behavior I wanted. The code you changed it to will use
person_id instead. Consider the situaion where you have a table with
mother_id and father_id which both point to the person table. With my
original code, you could write

(belongs-to mother :model person)
(belongs-to father :model person)

but with your code I have to write


(belongs-to mother :fk mother_id :model person)
(belongs-to father :fk father_id :model person)

That is why I have the extra case in the cond in the original code.
Did you change it because you disagree with my intention and want the
default fk to match the association name?

Jim

Jim Menard

unread,
Jun 14, 2010, 9:41:04 AM6/14/10
to clj-rec...@googlegroups.com
Hmm...the change you made doesn't break the tests. That means that the
tests I wrote aren't good enough. I'll try to make time to write some
more tests that show what I mean.

Jim

John D. Hume

unread,
Jun 14, 2010, 11:34:31 AM6/14/10
to clj-rec...@googlegroups.com
Jim,
You're right. I meant to use association-name rather than
associated-model-name as the fallback. We do still only need an (or
...) rather than the 3-branch (cond ...), but I kept the wrong branch.

--
http://elhumidor.blogspot.com/

Jim Menard

unread,
Jun 14, 2010, 12:07:28 PM6/14/10
to clj-rec...@googlegroups.com
Thank you.

A git question. Either I'm crazy and it was association-name all
along, or you changed associated-model-name to association-name
without creating a new git commit. How did you do that?

Jim

Reply all
Reply to author
Forward
0 new messages