
Self-Referential has many doesn't works.
Reported by Mauricio Eduardo Szabo | September 30th, 2009 @ 10:21 AM | in 0.10.2
Well, I just followed the site's tutorial on Self-Referential Has, and belongs to, many, but when I try to auto-migrate the column, it gives me an error. The code I'm trying to use is:
require 'dm-core'
class User
include DataMapper::Resource
property :id, Serial
property :name, String, :nullable => false
has n, :friended_users
has n, :friends, :through => :friended_users, :class_name => "User",
:child_key => [:user_id]
has n, :friended_by, :through => :friended_users, :class_name => "User",
:remote_name => :user, :child_key => [:friend_id]
end
class FriendedUser
include DataMapper::Resource
property :user_id, Integer, :key => true
property :friend_id, Integer, :key => true
belongs_to :user, :child_key => [:user_id]
belongs_to :friend, :class_name => "User", :child_key => [:friend_id]
end
DataMapper.setup :default, 'sqlite3::memory:'
DataMapper.auto_migrate!
and the error:
from /usr/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/property_set.rb:169:in `map'
from /usr/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/property_set.rb:169:in `initialize'
from /usr/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/associations/many_to_many.rb:23:in `new'
from /usr/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/associations/many_to_many.rb:23:in `child_key'
from /usr/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/model.rb:695:in `assert_valid'
from /usr/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/model.rb:694:in `each_value'
from /usr/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/model.rb:694:in `assert_valid'
from /usr/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/model.rb:693:in `each_value'
... 6 levels...
from /usr/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/migrations.rb:48:in `repository_execute'
from /usr/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/migrations.rb:36:in `auto_migrate_down!'
from /usr/lib/ruby/gems/1.8/gems/dm-core-0.10.0/lib/dm-core/migrations.rb:21:in `auto_migrate!'
from /tmp/test.rb:27
Comments and changes to this ticket
-
haarmann (at recordjob) October 1st, 2009 @ 05:38 AM
same here, the error is
dm-core-0.10.1/lib/dm-core/property_set.rb:169:in
initialize': undefined method
name' for nil:NilClass (NoMethodError)any help appreciated.
-
Dan Kubb (dkubb) October 7th, 2009 @ 01:18 PM
- Milestone set to 0.10.2
- State changed from new to accepted
- Assigned user set to Dan Kubb (dkubb)
-
Mislav November 1st, 2009 @ 08:42 AM
I tried a simpler approach in 0.10.1, but the JOIN doesn't alias the second occurrence of "users" table so it can't possibly work. I guess subselects in 0.10.2 will solve this
-
Dan Kubb (dkubb) November 10th, 2009 @ 03:41 AM
@Mislav: Yes, I would expect the change to subselects will resolve this problem, while also opening up more complex queries than could reasonably be formed using the INNER JOIN syntax. I have been using subselects in some cases, but have not switched M:M associations over to using them. I expect to be able to get this done soon tho.
-
Mislav December 17th, 2009 @ 05:38 PM
- Tag set to many-to-many, self-referential
0.10.2 is out, I hoped that this will get in.
I have an app with self-referential many-many (following/followers) and I have to go through ugly hacks to be able to make queries at all.
I'll try to help make some failing tests when I have time.
-
Dan Kubb (dkubb) December 30th, 2009 @ 03:49 PM
Attached is a script that works with forthcoming changes (today).
Note that you can't specify :child_key or :parent_key on a m:m relationship. They have to be specified on the relationship that is actually linking to the join table.
-
-
Dan Kubb (dkubb) December 30th, 2009 @ 04:10 PM
- State changed from accepted to resolved
(from [4cb77c36e1f16a70facd853ad5c7298d24746eaa]) Fix bug with self referential many to many relationships
- Alias tables in m:m queries when necessary
- Move the query conditions from the relationships from the WHERE clause to the ON clause.
- NOTE: need to clean up SQL generation soon. This was a really painful fix, and the code needs major refactoring in order to resolve other outstanding tickets.
[#902 state:resolved] [#1065 state:resolved] http://github.com/datamapper/dm-core/commit/4cb77c36e1f16a70facd853...
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
People watching this ticket
Attachments
Referenced by
-
902 Self Relational Many to Many Association not working [#902 state:resolved] [#1065 state:resolved] http://gith...
-
1065 Self-Referential has many doesn't works. [#902 state:resolved] [#1065 state:resolved] http://gith...