-1

my knowledge about database driven app and database ORM is wee, I have written this model in peewee https://codereview.stackexchange.com/q/210293/22943

and I want to be able to update the 3 tables Patient, Relative and PatientAttendOnVisit at the same time with Relative and PatientAttendOnVisit foreign key to Patient ID in Patient table.

I tried straight forward:

def add_patient_visit(data=None):
    """
    Adds new visit to clinic of patient 
    for new or follow up patient.
    """
    if not data:
        raise ValueError("Please pass the user info.")
    try:
        patient = _clinic.Patient.get(name=data["name"])
        if patient:
            print "Patient exists with same name."
        response_object = {
        "status": "fail",
        "message": "Patient already in record."
        }
        return response_object, 400
    except peewee.DoesNotExist as er:
        patient = _clinic.Patient.create(
            name=data["name"],
            townCity=data["townCity"],
            contactnumber=data["contactnumber"],
            age=data["age"],
            gender=data["gender"],
            email=data["email"],
            postalAddress=data["postalAddress"])
        relative = _clinic.Relative.create(relation=data["relation"],
            relativeName=data["relativeName"])

        attendence = _clinic.PatientAttendOnVisit.create(
            dateTimeofvisit=data["dateTimeofvisit"],
            attendtype=data["attendtype"],
            department=data["department"]
            )

but attempting to do so give me below error:

return controller.add_patient_visit(data=data) File "/Users/ciasto/Development/python/clinic-backend/app/api/clinic/controller.py", line 35, in add_patient_visit relativeName=data["relativeName"]) File "/Users/ciasto/Development/python/clinic-backend/clinic_venv/lib/python2.7/site-packages/peewee.py", line 5580, in create inst.save(force_insert=True) File "/Users/ciasto/Development/python/clinic-backend/clinic_venv/lib/python2.7/site-packages/peewee.py", line 5727, in save pk_from_cursor = self.insert(**field_dict).execute() File "/Users/ciasto/Development/python/clinic-backend/clinic_venv/lib/python2.7/site-packages/peewee.py", line 1622, in inner return method(self, database, *args, **kwargs) File "/Users/ciasto/Development/python/clinic-backend/clinic_venv/lib/python2.7/site-packages/peewee.py", line 1693, in execute return self._execute(database) File "/Users/ciasto/Development/python/clinic-backend/clinic_venv/lib/python2.7/site-packages/peewee.py", line 2355, in _execute return super(Insert, self)._execute(database) File "/Users/ciasto/Development/python/clinic-backend/clinic_venv/lib/python2.7/site-packages/peewee.py", line 2118, in _execute cursor = database.execute(self) File "/Users/ciasto/Development/python/clinic-backend/clinic_venv/lib/python2.7/site-packages/peewee.py", line 2724, in execute return self.execute_sql(sql, params, commit=commit) File "/Users/ciasto/Development/python/clinic-backend/clinic_venv/lib/python2.7/site-packages/peewee.py", line 2718, in execute_sql self.commit() File "/Users/ciasto/Development/python/clinic-backend/clinic_venv/lib/python2.7/site-packages/peewee.py", line 2509, in exit reraise(new_type, new_type(*exc_args), traceback) File "/Users/ciasto/Development/python/clinic-backend/clinic_venv/lib/python2.7/site-packages/peewee.py", line 2711, in execute_sql cursor.execute(sql, params or ()) File "/Users/ciasto/Development/python/clinic-backend/clinic_venv/lib/python2.7/site-packages/MySQLdb/cursors.py", line 205, in execute self.errorhandler(self, exc, value) File "/Users/ciasto/Development/python/clinic-backend/clinic_venv/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler raise errorclass, errorvalue IntegrityError: (1452, 'Cannot add or update a child row: a foreign key constraint fails (clinic_backend.relative, CONSTRAINT relative_ibfk_1 FOREIGN KEY (patient_id) REFERENCES patient (id))')

Ciasto piekarz
  • 7,853
  • 18
  • 101
  • 197

1 Answers1

0

Nothing much complicated, I figured out,

def add_patient_visit(data=None):
    """
    Adds new visit to clinic of patient 
    for new or follow up patient.
    """
    if not data:
        raise ValueError("Please pass the user info.")
    patient = _clinic.Patient.create(
            name=data["name"],
            townCity=data["townCity"],
            contactnumber=data["contactnumber"],
            age=data["age"],
            gender=data["gender"],
            email=data["email"],
            postalAddress=data["postalAddress"])

        relative = _clinic.Relative.create(
            patient=patient,
            relation=data["relation"],
            relativeName=data["relativeName"])

        attendence = _clinic.PatientAttendOnVisit.create(
            patient=patient,
            dateTimeofvisit=data["dateTimeofvisit"],
            attendtype=data["attendtype"],
            department=data["department"]
            )
Ciasto piekarz
  • 7,853
  • 18
  • 101
  • 197