From 670b640c88d0dab31a0f4c9a9096e69b371bdd62 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Wed, 22 Jun 2011 17:18:41 -0400 Subject: [PATCH] - add doc example for column_property() on an association table per [ticket:1481] --- doc/build/orm/mapper_config.rst | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/doc/build/orm/mapper_config.rst b/doc/build/orm/mapper_config.rst index 259a6ecbab..d69cff6426 100644 --- a/doc/build/orm/mapper_config.rst +++ b/doc/build/orm/mapper_config.rst @@ -296,7 +296,22 @@ Correlated subqueries may be used as well:: ) }) -The declarative form of the above is described in :ref:`declarative_sql_expressions`. +For many-to-many relationships, use :func:`.and_` to join the fields of the +association table to both tables in a relation:: + + from sqlalchemy import and_ + + mapper(Author, authors, properties={ + '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 + ))) + }) + +For examples of :func:`.column_property` using Declarative, see +:ref:`declarative_sql_expressions`. Note that :func:`.column_property` is used to provide the effect of a SQL expression that is actively rendered into the SELECT generated for a -- 2.47.2