datamapper/dm-core — Issue #171: Problem with :belongs_to and legacy database that doesn't use "*_id" convention
Author: L2G
Created: 2011-12-20 06:11:54+00:00
State: open

I'm experimenting with the public domain MusicBrainz database and learning DataMapper at the same time. So I'm starting off slow:

```ruby
class Artist
include DataMapper::Resource

property :id, Serial
property :gid, String, :auto_validation => true

belongs_to :name, :model => 'ArtistName'
end

class ArtistName
include DataMapper::Resource

property :id, Serial
property :name, String
end
```

This would work great except for one thing: The parent table, "artist", really doesn't use the name "name_id" to link to the "artist_name" table; instead, it uses "name".

For the life of me I cannot work out any parameters to :belongs_to that will make this work. Adding :parent_key => 'name' fails because then it looks for a column named "name_name". Adding :child_key => 'name' errors out with the message "SystemStackError: stack level too deep".

I have a sneaky feeling the problem is the code in associations/many_to_one.rb that checks to see if the source key (a.k.a. parent key, yes?) has been renamed. But it's too late in the evening for me to wrap my brain around it. :-)