Specifying the table name for many-to-many relationships

19 views
Skip to first unread message

Paul Dlug

unread,
Dec 14, 2009, 2:27:32 PM12/14/09
to DataMapper
Is there an option to specify the name of the join table for many-to-
many relationships? I'm using models that are inside a module rather
than at the top level namespace (e.g. Product::User) and the generated
table name includes the module path (product_user_accounts). I'd like
to override this to something like just 'user_accounts' but I don't
see an option for doing this.

MarkMT

unread,
Dec 14, 2009, 4:25:49 PM12/14/09
to DataMapper
I think you just go -
has n, :things, :through => :the_model_name_of _your_choice

Paul Dlug

unread,
Dec 14, 2009, 4:27:35 PM12/14/09
to datam...@googlegroups.com
On Mon, Dec 14, 2009 at 4:25 PM, MarkMT <mark.t...@ieee.org> wrote:
> I think you just go -
> has n, :things, :through => :the_model_name_of _your_choice

That will specify the name of a relationship to use to access :things through.

MarkMT

unread,
Dec 14, 2009, 5:01:36 PM12/14/09
to DataMapper
Not unless I'm misunderstanding what you mean. It's the name of the
join model. See the second example here - http://datamapper.org/docs/associations.html.


On Dec 14, 3:27 pm, Paul Dlug <paul.d...@gmail.com> wrote:

Paul Dlug

unread,
Dec 14, 2009, 5:10:05 PM12/14/09
to datam...@googlegroups.com
On Mon, Dec 14, 2009 at 5:01 PM, MarkMT <mark.t...@ieee.org> wrote:
> Not unless I'm misunderstanding what you mean. It's the name of the
> join model. See the second example here - http://datamapper.org/docs/associations.html.

Right, it's the join *model*. This is for the case of a polymorphic
association where it is through a model like. My case is just a flat
many-to-many but I want to override the generated name of the table in
the association.

MarkMT

unread,
Dec 14, 2009, 6:36:59 PM12/14/09
to DataMapper
Ok, I guess I slightly misunderstood, but I still suspect that
the :through option is the closest solution to what you are looking
for. It sounds like you want DM (well rake) to automatically generate
a join table with a name that you choose, but you don't want to
declare a model for that table? AFAIK that isn't possible - if you
don't want to accept the default table name I think you have to define
a model for the table you actually want created.


On Dec 14, 4:10 pm, Paul Dlug <paul.d...@gmail.com> wrote:
> On Mon, Dec 14, 2009 at 5:01 PM, MarkMT <mark.thom...@ieee.org> wrote:
> > Not unless I'm misunderstanding what you mean. It's the name of the
> > join model. See the second example here -http://datamapper.org/docs/associations.html.

sliu

unread,
Dec 14, 2009, 8:52:18 PM12/14/09
to DataMapper
@Paul:
Do you mean this:

module Product
class UserAccount
include DataMapper::Resource

storage_names[:default] = 'user_accounts'
end
end


http://datamapper.org/why.html

Dan Kubb (dkubb)

unread,
Dec 14, 2009, 9:50:02 PM12/14/09
to DataMapper
Hi Paul,

> Is there an option to specify the name of the join table for many-to-
> many relationships?

No. The reason for :through => Resource is to provide a shortcut for
cases where the only reason the join model exists is to link the two
models *and* you're fine with the defaults for everything. At the
moment I can't see extending it with extra options, since that would
defeat the purpose of a shortcut.

In this case you would explicitly define a join model, and then
specify a has() association to it, and then use a second has()
with :through to join through it to the target. Since you define an
explicit join model, you can name it so that the generated table
matches your preferences, or you can use storage_names in the model
declaration to override the table name, eg:

class Product::User
# ...

has n, :account_users
has n, :accounts, :through => :account_users
end

class Product::AccountUser
include DataMapper::Resource

storage_names[:default] = 'account_users'

property :account_id, Integer, :min => 1, :key => true
property :user_id, Integer, :min => 1, :key => true

belongs_to :account
belongs_to :user
end

class Product::Account
# ...

has n, :account_users
has n, :users, :through => :account_users
end

--

Dan
(dkubb)

Paul Dlug

unread,
Dec 15, 2009, 3:03:04 PM12/15/09
to DataMapper
Thanks for the great explanation, that solved my problem perfectly.
Reply all
Reply to author
Forward
0 new messages