Hi, I have a query which works fine through the ORM, albeit a bit slow, so I want to drop down to the SQL layer to speed things up. However, being an SQL novice, I can't seem to figure out how to get it to work.
I have a database containing the history of repetitions with a flash card program. I have a table containing the flash cards, and a second table containing the details of all the repetitions for all the flashcards. I want to look at all cards where repetition 5 obeys certain criteria, and then for these cards look at repetitions 1 to 4. Using the ORM, that's easy enough: repetitions = db_session.query(Repetition).filter((Repetition.rep_number==5) \ & (Repetition.grade==2)) for r in repetitions: for index in range(4): print index, r.card.repetitions[index].grade After browsing through the documentation, I have the impression that using aliases could be the way to go. I tried the following code (which would only give me repetition 1 instead of 1 to 4, but a loop is easily made): repetition_table_2 = repetition_table.alias() s = select([repetition_table.c.grade], (repetition_table.c.rep_number==1) & \ (repetition_table.c.card_key==repetition_table_2.c.card_key) & \ (repetition_table_2.c.rep_number==5) & \ (repetition_table_2.c.grade==2)) However, this runs forever, so I'm guessing it escapes into the wonderful world of Cartesian products. Can anybody help me contruct the proper SQL query or otherwise speed up the ORM version? Thanks! Peter --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---