mmh, cant really grasp what u want to do. what are Line.* attributes? .filter does and(), so this can be simplified: q = query(Line).filter( models.Line.LineDiscarded == LineDiscarded ) if ...: return q.filter( sites...) else: return q.filter( sites...)
On Monday 19 January 2009 23:28:55 boudewijnector wrote: > Hi Guys, > > > Currently I'm maintaining a python-2.5 (pylons) webapplication, > which uses sqlaclhemy as its ORM. > I've been messing around for some time with this (quite stupid) > problem, but still can't het it to work the way I want it to. > > Situation: > > There's 2 tables, Fiber and Line. A line consists of a (or maybe in > the future, more) Fiber. > My definitions: > > fiberdb=# \d "Fiber"; > Table "public.Fiber" > Column | Type > > | Modifiers > > ------------------------+-------------------------- > +----------------------------------------------------------- > FiberID | integer | not null > default nextval('"Fiber_FiberID_seq"'::regclass) > LineName | character varying(50) | > FiberUsed | character(1) | > Indexes: > "PRI_Fiber" PRIMARY KEY, btree ("FiberID") > "FKI_Fiber_Line" btree ("LineName") > "fki_" btree ("LineName") > Foreign-key constraints: > "FK_Fiber_Line" FOREIGN KEY ("LineName") REFERENCES > "Line"("LineName") ON UPDATE CASCADE ON DELETE RESTRICT > > fiberdb=# \d "Line"; > Table "public.Line" > Column | Type > > Modifiers > ----------------------------+----------------------------- > +--------------------------------------------------------- > LineName | character varying(50) | not > null > > "PRI_Line" PRIMARY KEY, btree ("LineName") > > > > As you can see, pretty straightforward. > > > But, in my code I'd like to search for all Fibers which belong to a > group of Lines. The Lines are chosen by the user , and I'd like to > filter these Fibers depending on some conditions (like FiberUsed = > True). > > Because there might be a lot of branches for this search (as we add > options to the search engine, branches in the code will be added > to), I'd like to do the join seperately in the head of the function > and save the result of the join in some variable. > > This variable should be used in the branches of my code. This > allows me to easily change the join conditions without changing > the join in 10 branches of my code. > > My current code is like this: > > def _get_list_Lines_between_Sites(self, site1, site2, > LineDiscarded): > query = models.Session.query > (models.Line) > if site2 == > None: > return query.filter(and_ > (models.Line.LineDiscarded == LineDiscarded, or_ > (models.Line.SiteLocationCodeA == > site1,models.Line.SiteLocationCodeB == site1))) > else: > return query.filter(and_ > (models.Line.LineDiscarded == LineDiscarded, or_(and_ > (models.Line.SiteLocationCodeA == > site1,models.Line.SiteLocationCodeB == > site2),and_(models.Line.SiteLocationCodeA == > site2,models.Line.SiteLocationCodeB == site1) ))) > > > > The "query = " seems to be pretty invalid, but although I've tried > about 10 alternatives (3 parameter for the join, using quotes, not > using quotes, etc), I can't get this to work. > > Would anybody mind giving me a hand on this one? > > > Yours sincerely, > > Boudewijn Ector --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---