good idea I added a ticket for that.

bojanb wrote:
>
> There was an error in my posted code, I meant:
>
> 'book_count': column_property(select([func.count(books.c.id)],
> book_authors.c.author_id==authors.c.id).label('books_count'))
>
> but that's irrelevant.
>
> Using and_(), the *correct* definition is:
>
> 'book_count': column_property(select([func.count(books.c.id)], and_
> (book_authors.c.author_id==authors.c.id,
> book_authors.c.book_id==books.c.id)).label('books_count'))
>
> Thanks for the help. By the way, I suggest adding this example to the
> documentation; I know it would have saved me a lot of head-banging. I
> suggest the following text tacked at the end of "SQL Expressions as
> Mapped Attributes" in "Mapper Configuration":
>
> For many-to-many relationships, use and_() to join the fields of the
> association table to both tables in a relation:
>
> mapper(Author, authors, properties={
>     'books': relation(Book, secondary=book_authors,
> backref='authors'),
>     'book_count': column_property(select([func.count(books.c.id)], and_
> (book_authors.c.author_id==authors.c.id,
> book_authors.c.book_id==books.c.id)).label('books_count'))
>     })
>
>
> On Jul 22, 4:10 pm, "Michael Bayer" <[email protected]> wrote:
>> bojanb wrote:
>> > mapper(Book, books)
>> > mapper(Author, authors, properties={
>> >     'books': relation(Book, secondary=book_authors,
>> > backref='authors'),
>> >     'book_count': column_property(select([func.count(books.c.id)],
>> > book_authors.c.author_id==books.c.id).label('books_count'))
>> >     })
>>
>> the subquery within the column property is not joined to the parent
>> table
>> "authors".  It needs to join book_authors back to the authors table
>> using
>> AND.  the subquery will automatically correlate outwards to the
>> "authors"
>> query so "authors" would only render in the outermost FROM (which is
>> what
>> you want).
> >
>


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" 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/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to