[sqlalchemy] Re: Query.get with unordered multiple-column-primary-key
Hi Michael, 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... :) 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? -- Gaëtan de Mentenhttp://openhex.org --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: Query.get with unordered multiple-column-primary-key
Roger Demetrescu ha scritto: query.get(dict(columnB='foo', columnA='bar') Lazy programmers are the best ones... :) That's the reason lazy programmers share a superclass for all their domain objects... hint, hint :-) --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: Query.get with unordered multiple-column-primary-key
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.. 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: Query.get with unordered multiple-column-primary-key
On 6/13/07, Marco Mariani [EMAIL PROTECTED] wrote: Roger Demetrescu ha scritto: query.get(dict(columnB='foo', columnA='bar') Lazy programmers are the best ones... :) That's the reason lazy programmers share a superclass for all their domain objects... hint, hint :-) Yeaph, I totally agree.. :) But notice that this feature is related to SA's Query class, and not my domain objects... Unless I do something like (if it is possible at all): pseudo code class BaseClass(object) @staticmethod def give_me_pk_values_in_correct_order(self, **kw): table = don't know how to retrieve the correct table object return [kw[c.key] for c in table.primary_key] class Customer(BaseClass): pass /pseudo code But the use of this function is to ugly to my taste (I know, the give_me_pk_values_in_correct_order is too big here): customer = session.query(Customer).get(Customer.give_me_pk_values_in_correct_order(dict(columnX=3, columnY=4, columnZ=343)) Having to repeat Customer reference twic annoys me... :) Or am I missing a better way of doing this ? Cheers, Roger --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: Query.get with unordered multiple-column-primary-key
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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: Query.get with unordered multiple-column-primary-key
On Jun 13, 2007, at 8:48 AM, Roger Demetrescu wrote: But the use of this function is to ugly to my taste (I know, the give_me_pk_values_in_correct_order is too big here): customer = session.query(Customer).get (Customer.give_me_pk_values_in_correct_order(dict(columnX=3, columnY=4, columnZ=343)) Having to repeat Customer reference twic annoys me... :) its python ! its, there should be only one way to do it, we're taking away map() and reduce() because you already have list comprehensions.theres all kinds of things you might want to pass to get(), how would I know how you want to translate it ? --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: Query.get with unordered multiple-column-primary-key
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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: Query.get with unordered multiple-column-primary-key
On 6/13/07, Roger Demetrescu [EMAIL PROTECTED] wrote: Hi Michael, On 6/13/07, Michael Bayer [EMAIL PROTECTED] wrote: On Jun 13, 2007, at 8:48 AM, Roger Demetrescu wrote: But the use of this function is to ugly to my taste (I know, the give_me_pk_values_in_correct_order is too big here): customer = session.query(Customer).get (Customer.give_me_pk_values_in_correct_order(dict(columnX=3, columnY=4, columnZ=343)) Having to repeat Customer reference twic annoys me... :) its python ! its, there should be only one way to do it, we're taking away map() and reduce() because you already have list comprehensions.theres all kinds of things you might want to pass to get(), how would I know how you want to translate it ? Well, from my understanding, the spirit of get is to fetch something that you already know the primary key for, so supporting them in the form of a dictionary seem pretty natural to me. Indeed different people will have different opinions how things should work In my case, passing a dict() in query.get() was so intuitive to me, that I got a little frustrated when noticed it didn't work ... Same here. If query.get() is smart to deal with a sequence (eg: list or tuple), why it shouldn't be with a mapping ? :) Anyway, a couple of custom utilities functions will definitely do the job here.. Indeed. No worries, I'll survive this... It's not something I come across that often anyway. It just felt natural to be able to do it. -- Gaëtan de Menten http://openhex.org --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---