#923 ✓not-applicable
Matt Savona

Something funky about :child_key/:target_key in Many-to-Many relationship

Reported by Matt Savona | June 25th, 2009 @ 02:53 PM | in 0.10.0

The following code snippet is capable of reproducing the issue (against the 0.10 gems @ gems.datamapper.org):

http://pastie.org/private/y8lhqq3v3ugf0pwa34w1gq

Changing :child_key to :target_key alters the exception that is raised, but the issue remains.

It seems as if :child_key is not being respected in this case, and we make it into DataMapper::Associations::ManyToMany::Relationship#child_key, where we attempt to create a new PropertySet where the local child_key variable is Nil.

I think the larger issue is that we probably should be in that block to begin with, @child_key should already be defined.

This relationship (as defined the example above) does work in previous versions of DataMapper.

Comments and changes to this ticket

  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) June 27th, 2009 @ 04:05 AM

    • Assigned user set to “Dan Kubb (dkubb)”
    • State changed from “new” to “accepted”
  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) July 1st, 2009 @ 05:06 PM

    • State changed from “accepted” to “not-applicable”

    I think if :child_key worked that way in previous versions of DataMapper it was a bug.

    The only time when :child_key or :parent_key should be specified for a m:m relationship uses an anonymous join resource, which happens when you specify :through => Resource. The reason for this is that the join resource needs to create relationships between it and the source and target models.

    The whole point of an explicit :through association is that you are using the keys and other conditions defined in the relationship you are going "through". If you need to use different keys, then you should be going through a different relationship.

  • Matt Savona

    Matt Savona July 2nd, 2009 @ 10:28 AM

    With the :child_key statements in place, against DM 0.9.10, this is the result of my example:

    Thu, 02 Jul 2009 00:16:07 GMT ~ debug ~ (0.001831) SELECT main_groups.id, main_groups.name FROM main_groups INNER JOIN main_group_heritages ON (main_groups.id = main_group_heritages.child_group_id) WHERE (main_group_heritages.parent_group_id = 1) GROUP BY main_groups.id, main_groups.name ORDER BY main_groups.name
    [#<Main::Group id=2 name="Child">] Thu, 02 Jul 2009 00:16:07 GMT ~ debug ~ (0.001154) SELECT main_groups.id, main_groups.name FROM main_groups INNER JOIN main_group_heritages ON (main_groups.id = main_group_heritages.parent_group_id) WHERE (main_group_heritages.child_group_id = 2) GROUP BY main_groups.id, main_groups.name ORDER BY main_groups.name
    [#<Main::Group id=1 name="Parent">]

    With :child_key removed, in both 0.9.10 and 0.10, the WHERE clause includes main_group_heritages.group_id (where the heck is that coming from? that property (group_id) does not exist in my model definition):

    Thu, 02 Jul 2009 00:16:36 GMT ~ debug ~ (0.001040) SELECT main_groups.id, main_groups.name FROM main_groups INNER JOIN main_group_heritages ON (main_groups.id = main_group_heritages.group_id) WHERE (main_group_heritages.group_id = 1) GROUP BY main_groups.id, main_groups.name ORDER BY main_groups.name
    [] Thu, 02 Jul 2009 00:16:36 GMT ~ debug ~ (0.000969) SELECT main_groups.id, main_groups.name FROM main_groups INNER JOIN main_group_heritages ON (main_groups.id = main_group_heritages.group_id) WHERE (main_group_heritages.group_id = 2) GROUP BY main_groups.id, main_groups.name ORDER BY main_groups.name
    []

  • Matt Savona

    Matt Savona July 2nd, 2009 @ 10:30 AM

    Sorry, that was formatted poorly. Here are the SQL statements from above:

    Thu, 02 Jul 2009 00:16:07 GMT ~ debug ~ (0.001831) SELECT `main_groups`.`id`, `main_groups`.`name` FROM `main_groups` INNER JOIN `main_group_heritages` ON (`main_groups`.`id` = `main_group_heritages`.`child_group_id`) WHERE (`main_group_heritages`.`parent_group_id` = 1) GROUP BY `main_groups`.`id`, `main_groups`.`name` ORDER BY `main_groups`.`name`
    [#<Main::Group id=2 name="Child">]
    Thu, 02 Jul 2009 00:16:07 GMT ~ debug ~ (0.001154) SELECT `main_groups`.`id`, `main_groups`.`name` FROM `main_groups` INNER JOIN `main_group_heritages` ON (`main_groups`.`id` = `main_group_heritages`.`parent_group_id`) WHERE (`main_group_heritages`.`child_group_id` = 2) GROUP BY `main_groups`.`id`, `main_groups`.`name` ORDER BY `main_groups`.`name`
    [#<Main::Group id=1 name="Parent">]
    

    and

    Thu, 02 Jul 2009 00:16:36 GMT ~ debug ~ (0.001040) SELECT `main_groups`.`id`, `main_groups`.`name` FROM `main_groups` INNER JOIN `main_group_heritages` ON (`main_groups`.`id` = `main_group_heritages`.`group_id`) WHERE (`main_group_heritages`.`group_id` = 1) GROUP BY `main_groups`.`id`, `main_groups`.`name` ORDER BY `main_groups`.`name`
    []
    Thu, 02 Jul 2009 00:16:36 GMT ~ debug ~ (0.000969) SELECT `main_groups`.`id`, `main_groups`.`name` FROM `main_groups` INNER JOIN `main_group_heritages` ON (`main_groups`.`id` = `main_group_heritages`.`group_id`) WHERE (`main_group_heritages`.`group_id` = 2) GROUP BY `main_groups`.`id`, `main_groups`.`name` ORDER BY `main_groups`.`name`
    []
    

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.

New-ticket Create new ticket

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

Pages