Javier Rojas wrote:
> Patient
> # pat_ID
>
> Symptom
> # symp_ID
>
> Patient-Symptom
> # pat_ID
> # symp_ID
> * Annotation

I can guess.  I am relatively new to this too, but I have gained some
experience trying to make my computer-generated SQL (SQLObject) match
with another person's computer-generated schemas (DBDesigner).

First, I think if you can use another table instead of the join table,
maybe you should.  The table could have four columns: the (SQLObject
implicit) id, the patient id, the symptom ID, and an optional
annotation.  You would have to keep it synchronized with the join
table, though.  If not, see my attempt below.

Also, I have not used it, but people say SQLAlchemy is better for
flexibility.  I am about to attempt a switch to it because of my own
problems.  SQLObject is excellent, but only if you have complete
control on the DB design and can work within its assumptions.

It ain't pretty.  You can use tg-admin to generate the tables, then
dump the DB and manually add the column you need.  You only need to do
this once.  If you ever want a new fresh DB, you use your dump instead
of tg-admin.

class Patient(SQLObject):
    name = StringCol()
    symptoms = RelatedJoin('Symptom',
intermediateTable="patient_symptom",
                           joinColumn="patient_id",
otherColumn="symptom_id")

class Symptom(SQLObject):
    desc = StringCol()
    patients = RelatedJoin('Patient',
intermediateTable="patient_symptom",
                           joinColumn="symptom_id",
otherColumn="patient_id")

#class PatientWithSymptom(SQLObject):
#    class sqlmeta:
#        table = 'patient_symptom'
#    patient = ForeignKey('Patient')
#    symptom = ForeignKey('Symptom')
#    annotation = StringCol()

Next, tg-admin sql create.  SQLObject creates table patient_symptom
with only two columns because of the RelatedJoin fields.  I used
SQLite, so after that, I ran "sqlite3 /path/to/my/db .dump >
db-init.sql".

You can edit db-init.sql, find the patient_symptom table, and add the
following (and maybe an extra comma):
    id INTEGER PRIMARY KEY,
    annotation TEXT

Having fixed that (and having become non-portable), delete the old
database and create a new one from db-init.sql instead.  Uncomment the
PatientWithSymptom class and run tg-admin shell.  You can do something
similar to this:

>>> me = Patient(name='Jason Smith')
>>> figs = Symptom(desc='Has figs')
>>>
>>> # I got figs.
>>> me.addSymptom(figs)
>>>
>>> # Make an annotation about my figs problem.
>>> myProblem = 
>>> PatientWithSymptom.select(AND(PatientWithSymptom.q.patientID==me.id, 
>>> PatientWithSymptom.q.symptomID==figs.id))[0]
>>> myProblem
<PatientWithSymptom 1 patientID=1 symptomID=1 annotation=None>
>>> myProblem.annotation = "Yes, it's true!"
>>> hub.commit()

You can do a DB dump or SELECT to confirm that the annotation is stored
in the join table.  If it were me, there is no way I would use this for
real work.  But maybe it can serve as a fun example for somebody.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/turbogears
-~----------~----~----~----~------~----~------~--~---

Reply via email to