Michael Bayer ha scritto:
> heres two examples from the current unit tests:
>
> # tables:
>
> users = Table('users', metadata,
> Column('user_id', Integer, Sequence('user_id_seq', optional=True),
> primary_key = True),
> Column('user_name', String(40)),
> )
>
> addresses = Table('email_addresses', metadata,
> Column('address_id', Integer, Sequence('address_id_seq',
> optional=True), primary_key = True),
> Column('user_id', Integer, ForeignKey(users.c.user_id)),
> Column('email_address', String(40)),
>
> )
>
> # classes:
>
> class User(object):pass
> class Address(object):pass
>
> # mappers:
>
> mapper(User, users, properties={
> 'addresses':relation(Address, lazy=True)
> })
> mapper(Address, addresses)
>
> sess = create_session()
>
> # build a query with Query, which will select "users" plus
> "count(addresses.address_id) AS count"
>
> q = sess.query(User)
> q = q.group_by([c for c in
> users.c]).order_by(User.c.user_id).outerjoin('addresses').add_column(func.count(addresses.c.address_id).label('count'))
> l = q.list()
>
> # build a query with a Select statement which will select "users" plus
> "count(addresses.address_id) AS count"
>
> s = select([users, func.count(addresses.c.address_id).label('count')],
> from_obj=[users.outerjoin(addresses)], group_by=[c for c in users.c],
> order_by=[users.c.user_id])
> q = sess.query(User)
> l = q.instances(s.execute(), "count")
>
> in both cases, the results look like:
>
> [(user7, 1),
> (user8, 3),
> (user9, 0)]
>
> where the first member of the tuple is a User object, the second
> member is the value of "count(addresses.address_id)". with the Query,
> add_column() and add_entity() can be used repeatedly. with instances,
> you just add any number of column names, Column instances, or mappers
> to the positional arguments of instances().
>
Probably there is something i cannot understand....
I've tryed the second manner...with the select statement... but i see
something strange...
In [1]: t = TipoFigura()
In [1]: t.mapper.mapped_table
Table('tipo_figura',DynamicMetaData(),
Column('codice',PGChar(length=1),primary_key=True,nullable=False),
Column('descrizione',PGText(length=None),nullable=False),schema=None)
In [2]: s = select( [t.c.codice], from_obj=[t.mapper.mapped_table],
use_labels=True)
In [3]: ret = session.query( t.mapper )
In [4]: ret.instances( s.execute() )
2007-04-03 11:27:26,575 sqlalchemy.engine.base.Engine.0x..74 INFO SELECT
tipo_figura.codice AS tipo_figura_codice
FROM tipo_figura
_/home/xxxx/build/bdist.linux-i686/egg/sqlalchemy/engine/base.py in
_convert_key(self, key)
NoSuchColumnError: "Could not locate column in row for column
'tipo_figura.descrizione'"
_
1) Why this error?? the query is perfect and there is n reason for
check presence of the "descrizione" column. (the problem is the same i
found in previous mail)
2) and why if i put all columns i lost use_label features ?
s = select( [t.c.codice, t.c.descrizione],
from_obj=[t.mapper.mapped_table], use_labels=True)
ret.instances( s.execute() )[0].c.keys()
2007-04-03 11:39:56,921 sqlalchemy.engine.base.Engine.0x..74 INFO SELECT
tipo_figura.codice AS tipo_figura_codice, tipo_figura.descrizione AS
tipo_figura_descrizione
FROM tipo_figura
2007-04-03 11:39:56,921 sqlalchemy.engine.base.Engine.0x..74 INFO {}
Out[15]: ['codice', 'descrizione']
Thank
Glauco
--
+------------------------------------------------------------+
Glauco Uri - Programmatore
glauco(at)allevatori.com
Sfera Carta Software® [EMAIL PROTECTED]
Via Bazzanese,69 Casalecchio di Reno(BO) - Tel. 051591054
+------------------------------------------------------------+
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---