Agreed, there's several bad problems with JOINs in DM1, which are all Very Hard to solve due to DM's legacy complexity.
FWIW, fail cases + a wrie-up of one solution: https://gist.github.com/277406, and an (incomplete) attempt to implement it (in DM1): https://github.com/jpr5/dm-core/tree/fix_joins . We really should put up a doc about this somewhere on datamapper.org, so it's not a surprise to users.. Best way to solve these issues is using a relational algebra + AST, so yeah the time+place to do this is DM2. cheers, --jordan On Dec 7, 2011, at 1:49 PM, Chris Corbyn wrote: > I would expect a Collection of the Model from which the query was executed, > with any additional fields discarded. > > What about all users who have never posted? I know this works right on the > surface now, but it's hugely inefficient to the point it could never be used > in production. > > SQL: > > SELECT users.* FROM users LEFT JOIN posts ON posts.user_id = users.id WHERE > posts.id IS NULL > > (DataMapper builds a gigantic IN clause, which obviously you'd have to > optimise out in production where you're looking at millions of rows) > > There are certainly other valid cases for left join being used as part of a > more complex constraint. My issue is that the ORM wants to believe it should > know better than the person trying to extract the data, which is basically a > broken abstraction. I love DM, but it does run us into a corner quite often > so we're holding out for v2 and hopefully we'll have a clearer idea of what > the future is looking like for queries where you actually need to manipulate > the join behaviour yourself. > > > On 08/12/2011, at 8:40 AM, Jordan Ritter wrote: > >> >> What type of concrete objects would you expect the ORM layer to return, if >> such a method call existed? Outer joins tend to be used for getting mixed >> tuples of data that don't correspond to concrete ORM types. I can't >> immediately fathom what any ORM's behaviour should be.. >> >> What do you mean by "without talking to the data-store directly"? The only >> way I see to do this is to pass the DM adapter the SQL directly, and process >> the responses as abstract tuples of data (you'll get back an array). You >> could hydrate objects yourself manually from there, but YMMV: with any kind >> of outer join you can end up with nulls, and extra >> check-fields-before-hydration logic would be crappy to deal with (and why >> bother - this is DM's job to begin with). >> >> FWIW, you might consider using direct SQL to conduct your outer join(s) and >> only retrieve primary keys, then use the ORM to hydrate the objects akin to >> User.all(:id => [..]). >> >> cheers, >> --jordan >> >> >> On Dec 7, 2011, at 1:07 PM, terminal_breaker wrote: >> >>> Is there a way to do left outer joins in Datamapper without having to >>> resort to talking to the data-store directly. I've been searching the >>> net and the Datamapper docu with not success on details how to do so. >>> >>> For example: >>> >>> SELECT p.user_id, p.description, u.user_name >>> FROM privileges p >>> LEFT OUTER JOIN users u ON (p.user_id = u.id) >>> WHERE p.description = "delete rights" >>> >>> And let's say that I have a User class and a Privilege class too. >>> >>> >>> -- >>> 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. >>> >> >> >> -- >> 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. > > > -- > 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. -- 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.
