On 21.10.2008, at 20:22, Michael Bayer wrote: > > There's a couple of odd practices here, and removing each one > individually may narrow down where things are going wrong. > > 1. What happens if you use the correct class, patient.op_ci_implant(), > instead of "chartitem" which does not match the given type of > "op_ci_implant" ? > 2. Why does the primaryjoin condition for "chart" include a foreign > key value of 0 ? Note that SQLA probably cannot handle primary key > values of "0" at this time. > 3. Does the chartitem object attached to testpat.chart[0] have a > primary key or was that not flushed properly either ? What happens > if you query for the parent chartItem directly and do not include > "patient" in the operation ? Simplifying each step and asserting > sanity on each one is the key to isolating where something is going > wrong.
Thank you very much for answering Micheal As for 1. It produces the same error if I use the patient.op_ci_implant. I forgot to put it back in, after I experimented with chartItem. 2. I use patient_chart.c.Parent==0 to exclude all non "root" items from patient.chart[]. But this does not seem to be the error. The simpler test file I created does not include it and still the same problem. But later on more to this topic. 3. testpat.chart[0] has the correct foreign key. I have reimplemented the problem in a simpler, easier to understand source file. I have appended it to this mail. I really hope that somebody can help. This is driving me nuts! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
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), ) # contains the info about examinations examination_table = Table('exams', metadata, Column('id', Integer, ForeignKey('patientChart.id'), primary_key=True, nullable = False), Column('date', DateTime), ) metadata.create_all(engine) class patient(object): class chartItem(object): pass class exam(chartItem): pass mapper(patient.chartItem, patientChart_table, polymorphic_on=patientChart_table.c.type, polymorphic_identity='type', properties={ 'id': patientChart_table.c.id, 'patID': patientChart_table.c.patID, 'parent': patientChart_table.c.parent, 'type': patientChart_table.c.type, 'children': relation(patient.chartItem) }) mapper(patient.exam, examination_table, inherits=patient.chartItem, polymorphic_identity='exam', properties={ 'id': examination_table.c.id, 'date': examination_table.c.date, }) mapper(patient, patient_table, properties={ 'id': patient_table.c.patID, 'name': patient_table.c.name, 'chart': relation(patient.chartItem) }) testpat = patient() testpat.name = "Test Patient" session.add(testpat) testpat.chart.append(patient.exam()) testpat.chart.append(patient.exam()) session.commit() print testpat.chart[0].id, testpat.chart[0].patID print testpat.chart[1].id, testpat.chart[1].patID testpat.chart[0].children.append(patient.exam()) session.commit()