Hi. I am pretty new to SQLAlchemy, so apologize me if this is an easy one. :-)
I have three tables/classes (and their mappings), representing pictures (P), information of pictures (D), and information categories (C), respectively. D has many-to-one relationships with both P and C, so it's basically an association of P and C, having its own data too. When my system populates the database, it starts by creating instances of P. Adding instances of D to each P is done according to the P.all_my_d.append(...)-pattern where "all_my_d" is a one-to-many relationship between P and D. This is very elegant. However, an instance of D also needs a reference to an instance of C. If the appropriate instance of C exists in the database (or is pending to go into it), I want to pick this one, or otherwise create a new instance of C. What I am looking for is the most clean way to achieve this. I have run into trouble trying to use session.get(C, c_id) to lookup instances of C that have not been flushed yet. (I guess this might have something to do with primary keys not working before instances have become persistent?) Furthermore, I want the constructor of D to be where I lookup or create the appropriate instance of P, which seems to require that I pass the session object to the constructor so it can use session.get to look for an existing instance of P. I feel this passing of the session object around, is going to clutter the code, so I am looking for a cleaner way. It would be great if I could do something like this: ----8<---- #main program for p_data in external_data: session.save(P(p_data)) session.flush() # Now, all Ps, Ds and new Cs will be stored. class P(object): def __init__(self, p_data): # ... for (c_id, d_data) in p_data: self.all_my_d.append(D(c_id, d_data)) class D(object): def __init__(self, c_id, d_data): # ... # See if we find an instance of C having c_id as primary key. # If not, create a new one. self.my_c = new_or_existing_c_instance class C(object): def __init__(self, ...): # whatever ----8<---- where D.my_c is the many-to-one relationship between D and C. But what do I put into D.__init__ to achieve this in a clean way? (I can find a dirty way myself.) -- Sincerely, Knut Aksel Røysland --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---