"Arnar Birgisson" <[EMAIL PROTECTED]> writes: > Hi all, > > Say I have this simple model: > > DayTimesheet > date: a Date > entries: a list of TimeEntry > > TimeEntry: > day: a DayTimesheet > in: a time (HH:MM) > out: a time (HH:MM) > > DayTimesheet is the parent of TimeEntry in a one-to-many relationship. > > What would be the best way of ensuring the following: > > 1. Creating DayTimesheet on demand, i.e. > DayTimesheet.get_by(date=nonexistentdate) would create a new instance > if needed (inserted on the next flush())
You can try a "get_by" by that specific date and if there's an exception create the record on the except clause. > 2. Automatically removing childless DayTimesheet instances - that is, > when the last TimeEntry is deleted from a DayTimesheet, delete the > DayTimesheet too? I had a similar problem... I solved it by scheduling a query like: DELETE FROM parent_table WHERE id NOT IN (SELECT parent_id FROM child_table GROUP BY parent_id); It runs periodically and cleans the database from stale records. -- Jorge Godoy <[EMAIL PROTECTED]> --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---