On Jul 24, 2012, at 11:25 AM, Enrico Morelli wrote:

> Dear all,
> 
> I've the following tables:
> 
> site_table = Table('site', metadata,
>    Column('id', types.Integer, primary_key=True),
>    Column('name', types.Unicode(15), nullable=False))
> 
> chain_table = Table('chain', metadata,
>    Column('id', types.Integer, primary_key=True),
>    Column('letter', types.Unicode(1), nullable=False),
>    Column('ec_number', types.Unicode(50), nullable=True))
> 
> sites_chains_table = Table('sites_chains', metadata,
>    Column('site_id', types.Integer,
> ForeignKey('site.id'),primary_key=True),
>    Column('chain_id', types.Integer,ForeignKey('chain.id'),
> primary_key=True))
> 
> and the following mappers:
> 
> mapper(Site, site_table,
>       properties={'chain': relationship(Chain, backref='site',
>                    secondary=sites_chains_table), })
> 
> mapper(Chain, chain_table)
> 
> Which query I've to write to count all site where
> chain.ec_number==something using its relation.

query(Site).join(Site.chain).filter(Chain.ec_number==something).count()

see this recent stackoverflow answer:

http://stackoverflow.com/a/11568381/34549



> 
> I tried to do something like:
> count =
> Session.query(func.count(Site.id)).filter(Chain.ec_number==ec_number).filter(Site.chain.any(id=Chain.id)).all()
> 
> but the result isn't real.
> -- 
> -------------------------------------------------------------------
>       (o_
> (o_    //\  Coltivate Linux che tanto Windows si pianta da solo.
> (/)_   V_/_
> +------------------------------------------------------------------+
> |     ENRICO MORELLI         |  email: more...@cerm.unifi.it       |
> | *     *       *       *    |  phone: +39 055 4574269             |
> |  University of Florence    |  fax  : +39 055 4574253             |
> |  CERM - via Sacconi, 6 -  50019 Sesto Fiorentino (FI) - ITALY    |
> +------------------------------------------------------------------+
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> To unsubscribe from this group, send email to 
> sqlalchemy+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/sqlalchemy?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to