Polymorphic Associations

41 views
Skip to first unread message

Daniel N

unread,
Jun 12, 2008, 10:37:36 AM6/12/08
to datam...@googlegroups.com
Hi All,

I really need to get polymorphic assocaitions into my applicaiton, so I thought I'd start the ball rolling here.  I have no problems with having a crack at implementing them, but I'd like to try and define exactly what it *should* be.

With my very limited understanding of these things.  I'll put forward my thoughts to get the ball rolling. 

I'm thinking that schema wise, AR polymorphic associations are ok.  Lets say Comment has a polymorphic interface on :commentable


So.  Baically on the belongs_to side:

is :polymorphic, :commentable

would store a polymorphic interface on the Comment class.  Setup the commentable_type, and commentable_id properties. 

The polymorphic interface could be just a name, type_field_name, and id_field_name. 

On the has n side.

has n, :comments, :polymorphic => :commentable

would look at the class for the has assocaition for the :commentable polymorphic interface.  Then it sets the child keys to

[ polymorphic_interface.type_field_name => self.class, polymorphic_interface.id_field_name => self.id]

When the has n, is setup with the :commentable polymorphic, it should setup the belongs_to on the Comment class as well. 
The whole loading all from the Comment side is a bit I'm not sure on.  Especially if the polymorphic model is a join model and loading has n through.  I imagine it could get very messy at that point.

Does this make sense and would it work.  It doesn't *seem* conceptually difficult except for the has n through issue.

Please lets get some ideas on what ppl want to see in polymorphic joins.

Cheers
Daniel

Sam Smoot

unread,
Jun 12, 2008, 11:08:13 AM6/12/08
to DataMapper
Daniel,

I'm not so sure we want to use a schema similar to AR. Once upon a
time I was browsing over a Hibernate document on polymorphic
associations, and it seemed to make more sense schema-wise.

Unfortunately, I don't remember the details. :-/ I'll try to see if I
can dig 'em up.

Thanks, -Sam

Mark Bates

unread,
Jun 12, 2008, 11:25:50 AM6/12/08
to datam...@googlegroups.com
I definitely agree with Sam on this one. Let's see if there's a better
model out there for doing them. The company I work for uses them all
the time. Adam French and I had long discussions about polymorphic
associations at RailsConf, but we didn't really come up with anything
good. The one thing that really bugs my DBA is the string column for
the name of the class. We've hacked around it so we could use ints,
and we just keep a table that lists the classes, so we can do the
reverse mapping. It's not the best solution, but when you're using AR,
nothing really is. :)

-------------------------------------------------------------------------------------------------
Mark Bates
ma...@mackframework.com
http://www.mackframework.com
http://api.mackframework.com/
http://github.com/markbates/mack

Daniel N

unread,
Jun 12, 2008, 11:50:24 AM6/12/08
to datam...@googlegroups.com
On Fri, Jun 13, 2008 at 1:25 AM, Mark Bates <ma...@mackframework.com> wrote:

I definitely agree with Sam on this one. Let's see if there's a better
model out there for doing them. The company I work for uses them all
the time. Adam French and I had long discussions about polymorphic
associations at RailsConf, but we didn't really come up with anything
good. The one thing that really bugs my DBA is the string column for
the name of the class. We've hacked around it so we could use ints,
and we just keep a table that lists the classes, so we can do the
reverse mapping. It's not the best solution, but when you're using AR,
nothing really is. :)

-------------------------------------------------------------------------------------------------
Mark Bates
ma...@mackframework.com
http://www.mackframework.com
http://api.mackframework.com/
http://github.com/markbates/mack

Hey Mark,

What's the benefit of using ints for the class key?  Is there any reason why you would convert it to an int?

- Daniel 

Mark Bates

unread,
Jun 12, 2008, 11:54:19 AM6/12/08
to datam...@googlegroups.com
The benefit is efficiency in the database. If you have 10 million rows and they're all storing the value "User" that's a lot of data versus them all storing the int 1. That's the advantage, it's more performent and efficient. That's all.


-------------------------------------------------------------------------------------------------
Mark Bates



Daniel N

unread,
Jun 12, 2008, 11:56:26 AM6/12/08
to datam...@googlegroups.com


On Fri, Jun 13, 2008 at 1:54 AM, Mark Bates <ma...@mackframework.com> wrote:
The benefit is efficiency in the database. If you have 10 million rows and they're all storing the value "User" that's a lot of data versus them all storing the int 1. That's the advantage, it's more performent and efficient. That's all.


-------------------------------------------------------------------------------------------------
Mark Bates


Cool,  Is that the only difference between this option and the rails schema?  

afrench

unread,
Jun 12, 2008, 6:53:11 PM6/12/08
to DataMapper
reference note:

SQLAlchemy, from our python friends, model polymorphic relationships
pretty well....by separating the polymorphic bits into a separate
table.

http://techspot.zzzeek.org/?p=13

I haven't made up my mind as to whether I like this approach or not,
but the author feels pretty strong about it "following the rules" of
good DB design and that it follows the "joined table inheritance"
pattern. I wasn't really aware of patterns in the DB world, but not a
huge surprise that they're there.

What do ya'll think of his approach?
===
~Adam (afrench)


On Jun 12, 10:56 am, "Daniel N" <has....@gmail.com> wrote:
> On Fri, Jun 13, 2008 at 1:54 AM, Mark Bates <m...@mackframework.com> wrote:
> > The benefit is efficiency in the database. If you have 10 million rows and
> > they're all storing the value "User" that's a lot of data versus them all
> > storing the int 1. That's the advantage, it's more performent and efficient.
> > That's all.
>
> > -------------------------------------------------------------------------------------------------
> > Mark Bates
> > m...@mackframework.com

Daniel N

unread,
Jun 12, 2008, 8:51:25 PM6/12/08
to datam...@googlegroups.com

Hey Adam,

Thanx for the link, that was a very interesting read, but it seems that they've only implemented as one to one relationship.  I think if it expanded to a has n it would suffer the same issues as an AR polymorphic join.

Cheers
Daniel

Adam French

unread,
Jun 14, 2008, 12:14:59 AM6/14/08
to datam...@googlegroups.com
dkubb's come up with a way of doing "pseudo-polymorphic joins" (the approach that hassox and I came up with the other night) using just the dm-core public api.


Reactions?
===
~Adam

(posted here for posterity: polymorphic associations polymorph relationship join)

Carl Lerche

unread,
Jun 15, 2008, 5:43:39 PM6/15/08
to DataMapper
Yeah, I don't know if polymorphic associations as such are really
needed. "pseudo-polymorphic joins" is probably enough for the most
part. It would even be possible to add a Comment#commentable method if
absolutely necessary.
Reply all
Reply to author
Forward
0 new messages