Hi, I have tried to implement all day now. The problem is, that the values of the parent chartItem are populated by sqlalchemy after a flush. But I would have to extract the value by the parent before a flush, otherwise the db complains becuause sqllite would also try to flush the child at the same time. I only see 2 ways out of this chicken / egg dilemma: 1. Somehow implement the treelike self referential mapper directly in the patient mapper (not in the patient.chartItem mapper) or 2. Do a huge tradeoff and sacrifice a clean db design so that only patID or parent have values in child items. I really hate option 2. So would it be possible to somehow implement option 1?
Regards, Hinrich Winther. On 22.10.2008, at 19:43, Michael Bayer wrote: > > oh, ok. Here's a simplified case: > > from sqlalchemy import * > from sqlalchemy.orm import * > > engine = create_engine('sqlite:///:memory:', echo=False) > Session = sessionmaker(bind=engine) > session = Session() > metadata = MetaData() > > # this table contains the base data for a patient, like his name, > phone number ... > patient_table = Table('patient', metadata, > Column('patID', Integer, primary_key=True, > nullable = False), > Column('name', String), > ) > > # Stores data like which operations have been performed on a patient > or which exams have been performed ... > patientChart_table = Table('patientChart', metadata, > Column('id', Integer, primary_key=True, > nullable = False), > Column('parent', Integer, > ForeignKey('patientChart.id')), > Column('type', String), > Column('patID', Integer, > ForeignKey('patient.patID'), nullable = > False), > ) > > metadata.create_all(engine) > > class patient(object): > pass > > class chartItem(object): > pass > > mapper(chartItem, patientChart_table, > properties={ > 'children': relation(chartItem) > }) > > mapper(patient, patient_table, properties={ > 'chart': relation(chartItem) > }) > > testpat = patient() > testpat.name = "Test Patient" > > session.add(testpat) > testpat.chart.append(chartItem()) > testpat.chart.append(chartItem()) > > session.commit() > print testpat.chart[0].id, testpat.chart[0].patID > print testpat.chart[1].id, testpat.chart[1].patID > > c = chartItem() > > # there is no cascade from "patient" to the children of "chartItem" > c.patID = testpat.chart[0].patID > testpat.chart[0].children.append(c) > session.commit() > > There is no cascade of patient's "patID" column to objects that are > attached to its child "chart" objects, it only deals with the relation > to the chart items that are immediately associated with it. You'd > have to do that manually or add a before_save() hook to populate it. > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---