
Customization self-referencing association
Reported by sausheong (at gmail) | April 19th, 2009 @ 03:40 AM
In a data model where associations are customized, doing a query on a self-reference association causes a bug. For example, in this model:
class User
include DataMapper::Resource
property :id, Serial
property :name, String, :length => 255
end
class Message
include DataMapper::Resource
property :id, Serial
property :text, String, :length => 255
property :created_at, DateTime
belongs_to :recipient, :class_name => "User", :child_key => [:recipient_id]
belongs_to :user
end
Doing something like this:
Message.all('user.id' => user1.id, 'recipient.id' => user2.id)
results in this error:
MysqlError: (mysql_errno=1066, sql_state=42000) Not unique table/alias: 'users'
Query: SELECT `messages`.`id`, `messages`.`text`, `messages`.`created_at`, `messages`.`user_id`, `messages`.`recipient_id` FROM `messages` INNER JOIN `users` ON (`users`.`id` = `messages`.`user_id`) INNER JOIN `users` ON (`users`.`id` = `messages`.`recipient_id`) WHERE (`users`.`id` = 1) AND (`users`.`id` = 2) ORDER BY `messages`.`id`
This is the SQL that should have been generated:
SELECT `messages`.`id`, `messages`.`text`, `messages`.`created_at`, `messages`.`user_id`, `messages`.`recipient_id` FROM `messages` INNER JOIN `users` as u1 ON (u1.`id` = `messages`.`user_id`) INNER JOIN `users` as u2 ON (u2.`id` = `messages`.`recipient_id`) WHERE (u1.`id` = 1) and (u2.id = 2) ORDER BY `messages`.`id`
Comments and changes to this ticket
-
Dan Kubb (dkubb) May 28th, 2009 @ 07:19 PM
- State changed from unconfirmed to accepted
- Assigned user set to Dan Kubb (dkubb)
-
Dan Kubb (dkubb) June 16th, 2009 @ 03:18 AM
- Milestone set to 0.10.0
-
Dan Kubb (dkubb) October 4th, 2009 @ 09:33 PM
- Milestone changed from 0.10.0 to 0.10.2
[project:id#20609 not-tagged:"0.10.0" milestone:id#51895 bulk edit command]
-
Jox December 21st, 2009 @ 08:48 AM
- Tag changed from associations, finder, relationships to associations, finder, relationships, self-referential
-
Dan Kubb (dkubb) February 1st, 2010 @ 04:33 PM
- Milestone changed from 0.10.2 to 1.0.0
-
-
-
Dan Kubb (dkubb) February 2nd, 2010 @ 02:47 AM
- State changed from accepted to confirmed
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 »