Specifying the name of the join table in a many-to-many relationship

6 views
Skip to first unread message

fooksca

unread,
Nov 15, 2009, 1:34:19 PM11/15/09
to DataMapper
Hi,

I'm having problems working out how to specify the name of the join
table in a (non-) anonymous many-to-many relationship in an
unchangeable legacy database.

In SQL terms, I have three tables foo, bar and foo_bar (foo_bar being
the join table, and that only having foo_id and bar_id columns). All
table names are singular.

I have made several attempts to model the relationship, the closest I
can get it is with this:

class Foo
include DataMapper::Resource

storage_names[:default] = 'foo'

property :id, Serial
property :a_column, String

has n, :bars, :through => Resource
end

class Bar
include DataMapper::Resource

storage_names[:default] = 'bar'

property :id, Serial
property :a_column, String

has n, :foos, :through => Resource
end

Except the join table's name comes out as "bar_foos". Close!

I tried using "has n, :foos, :through => :foo_bars" and having the
foo_bars class as:

class FooBar
include DataMapper::Resource

storage_names[:default] = 'foo_bar'

belongs_to :foo
belongs_to :bar
end

But I'm told there's no key defined. Adding ":key => true" to both of
them doesn't work.

I looked through dm-core/associations/many_to_many.rb but there
doesn't seem to be a way to name it through options, and my Ruby isn't
good enough to discern how it does its magic! ;o)

Is it possible? How can I set the join table's name?

Cheers,
-Carl
Reply all
Reply to author
Forward
0 new messages