well, if u dont want to write the same thing over and over, write one 
wrapping function, and publish it here.

e.g. something like (pseudocode):
def _get_pk_ordered( klas):
   table = orm.mapper.registry(klas).mapped_table  #or select_table
   ....
   return whatever-list-of-columns

def get_by_pk( session, klas, **columns):
   cols = _get_pk_ordered( klas)
   assert len(cols) == len(columns)
   return session.query(klas).get( [columns[c.name] for c in cols])

i guess, if its really useful, it may even go into some extension/ 
module.


> > > Some of my coworkers had the same needs of Gaetan... And while
> > > I understand your solution, I figure out if SA could have it
> > > natively (detecting the presence of a dictionary)...
> > >
> > > Somethink like:
> > >
> > >
> > > query.get(dict(columnB='foo', columnA='bar')
> > > Lazy programmers are the best ones...  :)
> >
> > why not query.get(**dict(columnB='foo', columnA='bar')) ?
> > it should work as is..
>
> No, it doesn't...  :/

> I have the following code:
>
> duplicata_table = Table('DUPLICATA', metadata,
>     Column('dpl_loja', String(2), primary_key=True),
>     Column('dpl_id', Integer, Sequence('gen_dpl_id'),
> primary_key=True), # other columns
>     )
>
> mapper(Duplicata, duplicata_table, properties={
>     'loja': duplicata_table.c.dpl_loja,
>     'id': duplicata_table.c.dpl_id,
>     # other proprerties
>     })
>
> Now trying your suggestion:
> >>> d = session.query(Duplicata).get(**dict(loja='02', id=9))
>
> Traceback (most recent call last):
>   File "<pyshell#17>", line 1, in <module>
>     d = session.query(Duplicata).get(**dict(loja='02', id=9))
> TypeError: get() takes exactly 2 non-keyword arguments (1 given)
>
> It expects me to do :
> >>> d = session.query(Duplicata).get(['02', 9])
>
> Dealing with compound primary key of 2 columns is easy... but
> believe me: I have worked with some legacy database in the past
> which has tables with more than 9 columns in their primary key...
> !!
>

--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to