Thanks Michael! "Properly" was a poor choice of words on my part. Yes, of course, == is overloaded to make where (etc) clauses pretty, I just didn't put two and two together. --jack
On Wed, Jan 21, 2009 at 5:38 PM, Michael Bayer <mike...@zzzcomputing.com>wrote: > > > On Jan 21, 2009, at 7:46 PM, Jack Stahl wrote: > > > Hello, > > > > I've got a few SQL interfaces where I'd like to change my query > > based on the columns required by the client. For example, in one > > situation, I only join against my User table if my client requires a > > photo id: > > > > # cols is the list of columns the client would like selected > > if User.c.photo_id in cols: > > table = table.join(User.table) > > > > In another situation, I'd like to include the flags column in the > > query even if the client ask for it > > > > if not cols: > > cols = [cls] > > elif not (cls.c.flags in cols): > > # ensure flags are there so we can add "is_active" > > cols.append(cls.c.flags) > > > > However, my tests for membership pass regardless of whether column > > is actually in the list cols. That is, > > > > Column('flags', BitField(), table=<foo_table>) in [Column('foo', > > String(length=84, convert_unicode=False, assert_unicode=None), > > table=<foo_table>), Column('bar', Text(length=None, > > convert_unicode=False, table=<foo_table>)] > > > > evaluates to True > > > > Is the == operator not properly implemented for SQLAlchemy Columns? > > (I'm using version 0.42) > > it is not "properly" implemented in the sense that it does not return > True or False, but if you''ve used SQLalchemy, you'd know that we > redefine the "==" operator to return ClauseElement objects. That's > why you can say somecolumn==5 and get an expression from it. > > So you cannot use the "in" operator to search for a Column object in a > list. The solution however is simple. Use a set() instead, where the > objects will be compared based on the return value of __hash__() which > results in an object identity comparison. > > if User.c.photo_id in set(cols): > > > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---