On May 19, 2014, at 12:07 PM, George Reilly <[email protected]> wrote:
> > The code originally used > > Appointment.persons = relationship( > 'AppointmentPerson', > cascade='delete, save-update, merge, expunge', > lazy='dynamic') > > where everything worked, but accessing the persons on every appointment > triggered a separate query for AppointmentPersons, followed by > queries for each Person. > > Changing the relationship to `lazy='joined'` and using joinedload > > query = (session.query(Appointment). > options(joinedload(Appointment.persons). > joinedload(AppointmentPerson.person))) > return query.all() > > > However, with `lazy='joined'` and using joinedload, > I no longer know how to delete the AppointmentPersons > associated with an Appointment. `appt.persons.delete()` used to work; > now I get `AttributeError: 'InstrumentedList' object has no attribute > 'delete'` the "dynamic" relationship strategy replaces the usual simple Python collection with a special Query object, so when you call delete() on this Query object you get a DELETE statement immediately. When you go to any strategy other than "dynamic" however the relationship now acts like a simple Python collection, in this case a list, which has no delete() method nor does it have any linkage to a live SQL query. You can empty out the collection using a standard python idiom like myobject.stuff[:] = [] - if you then configure "delete-orphan" cascade those objects will be deleted on the next flush. Alternatively, you can get that direct delete() statement if you say sess.query(AppointmentPerson).with_parent(some_appointment).delete(). -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
