This isn't a show-stopper for me, as I was marvelling at DM's ability to find
records via relations more than one JOIN clause away, but I seem to have run
into a DM bug.
I have this pseudo legacy schema:
user (
userid INT PRIMARY KEY,
... snip ...
)
auction (
auctionid INT PRIMARY KEY,
userid INT,
... snip ...
)
auctionbid (
bidid INT PRIMARY KEY,
userid INT,
auctionid INT,
)
Now, a user owns many auctions, which in turn own many bids (owned by other
users). I was trying to see if DM could handle providing an accessor on the
User model for "received_bids", by using "has n, :through, :via":
class User
... SNIP ...
has n, :auctions, :child_key => [ 'userid' ]
has n, :received_bids, 'Bid', :through => :auctions, :via => :bids
... SNIP ...
end
It looks like the SQL it generates *would* work, only that DM is leaving
ambiguous field names in one of the JOIN clauses:
Column 'userid' in on clause is ambiguous (code: 1052, sql state: 23000, query:
SELECT `auctionbid`.`bidid`, `auctionbid`.`amount`, `auctionbid`.`status`,
`auctionbid`.`auctionid` FROM `auctionbid` INNER JOIN `auction` ON
`auctionbid`.`auctionid` = `auction`.`auctionid` INNER JOIN `user` ON
`auction`.`userid` = `userid` WHERE `auction`.`userid` = 97639 GROUP BY
`auctionbid`.`bidid`, `auctionbid`.`amount`, `auctionbid`.`status`,
`auctionbid`.`auctionid` ORDER BY `auctionbid`.`bidid`
The query should be (added `user` in the second JOIN clause):
SELECT `auctionbid`.`bidid`, `auctionbid`.`amount`, `auctionbid`.`status`,
`auctionbid`.`auctionid` FROM `auctionbid` INNER JOIN `auction` ON
`auctionbid`.`auctionid` = `auction`.`auctionid` INNER JOIN `user` ON
`auction`.`userid` = `user`.`userid` WHERE `auction`.`userid` = 97639 GROUP BY
`auctionbid`.`bidid`, `auctionbid`.`amount`, `auctionbid`.`status`,
`auctionbid`.`auctionid` ORDER BY `auctionbid`.`bidid`
Did I misuse the :through and :via options, or have I found a bug that I should
report? It's the first time I've tried to use such a complex association, and
DM appears to have the logic correct, but failing for a silly reason.
DM allows me to use a slightly longer way around for now, doing
User.auctions.bids, though this executes two queries, placing a large IN()
clause on the second query.
Cheers,
Chris
--
You received this message because you are subscribed to the Google Groups
"DataMapper" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/datamapper?hl=en.