To start, here's a summary of my data model:
class Tag
include DataMapper::Resource
storage_names[:default] = 'Tags'
#properties...
has n, :EntryTag, :child_key => [:Tag_Id]
has n, :entries, 'Entry', :through => :EntryTag
end
class EntryTag
include DataMapper::Resource
storage_names[:default] = 'Weblog_Entry_Tags'
belongs_to :tag, 'Tag', :key => true
belongs_to :entry, 'Entry', :key => true
end
class Entry
include DataMapper::Resource
storage_names[:default] = 'Weblog_Entries'
has n, :comments, 'Comment', :order =>
[:creation_dt_utc], :child_key => [:Entry_Id]
has n, :EntryTag, :child_key => [:Entry_Id]
has n, :tags, :through => :EntryTag, :order => [:title]
#properties...
end
Things that don't work as expected:
1. When I have a list of Entrys and get "entry.comments", DM helpfully
fetches all the associated comments for me. Not so with Tags. So DM
can avoid the n+1 query problem with one-to-many but not many-to-many
relationships?
2. To avoid the n+1 queries, I'd like to pre-fetch all the tags for
all the entries I've already retrieved. I'd expect to be able to query
like:
Tag.all(:entries => [list of entries])
or
Tag.all(:entries => {:id => [ids]})
The generated SQL for the former is:
>> Tag.all(:entries => [entries[0]])
~ Unknown column 'entry_id' in 'where clause' (code: 1054, sql state:
42S22, query: SELECT `Tag_Id`, `Name`, `Title`, `Description` FROM
`Tags` WHERE `entry_id` = 9331 ORDER BY `Tag_Id`, uri:
mysql://root@localhost/keithd_staging?charset=UTF-8)
DataObjects::SQLError: Unknown column 'entry_id' in 'where
clause' (code: 1054, sql state: 42S22, query: SELECT `Tag_Id`, `Name`,
`Title`, `Description` FROM `Tags` WHERE `entry_id` = 9331 ORDER BY
`Tag_Id`
In other words, it's not going through the "through" table.
For the latter query:
>> Tag.all(:entries => {:id => [9332]})
~ (0.000611) SELECT `Weblog_Entries`.`Entry_Id` FROM `Weblog_Entries`
INNER JOIN `Weblog_Entry_Tags` ON `Weblog_Entries`.`Entry_Id` =
`Weblog_Entry_Tags`.`entry_id` INNER JOIN `Tags` ON
`Weblog_Entry_Tags`.`tag_id` = `Tags`.`Tag_Id` WHERE
`Weblog_Entries`.`Entry_Id` IN (9332)
~ (0.000323) SELECT `Tag_Id`, `Name`, `Title`, `Description` FROM
`Tags` WHERE `Tag_Id` = 9332 ORDER BY `Tag_Id`
=> []
And that's not right at all.
Am I querying or configuring wrong or does DataMapper not handle these
types of things?
Thanks!
--
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.