On 6/13/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > > > > > 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... !! I wrote my own ORM in that occasion, and remember that passing primary keys values was a pain in the a**... :) To avoid having to pass 10 positional parameters, I made use of records (Delphi was my language)... just to make it possible to name each parameter (the same thing python give us with dict()) So, if I was to use SA with that database, I would have to make some utility functions like: def pkvalues(table, **kw): return [kw[c.key] for c in table.primary_key] and do search in those monster tables ('monster' because of its primary key): m = session.query(Monster).get(**pkvalues(monster_table, col1=23, col2='dsfs', col3=7, etc...)) The point is: if SA already knows witch table is associated with each ORM class, why not putting that function inside it (in Session.get() or a better place) ? And when I say "witch table", it's because of the "complexity" of using polymorphic (which I haven't used it yes, but have the desire to) But maybe I am over complicating things... :) Cheers, Roger > > > > > > On 6/12/07, Michael Bayer <[EMAIL PROTECTED]> wrote: > > > look through the keys in [c.key for c in table.primary_key], > > > match those up > > > > > > i.e. > > > > > > query.get(*[mydict[c.key] for c in table.primary_key]) > > > > > > On Jun 12, 1:07 pm, "Gaetan de Menten" <[EMAIL PROTECTED]> > wrote: > > > > Hi, > > > > > > > > Anybody knows how I could emulate the behavior of Query.get (ie > > > > get the result from the session if possible instead of always > > > > fetching from the db) if I have the values for the different > > > > columns of the primary as keyword arguments (ie not in the > > > > order of the columns of the initial table)? I need a kind of a > > > > mix between get_by and get. Any idea? > > > > > > > > -- > > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---