如何在peewee的多个表中创建记录?

nbewdwxp  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(417)

我对数据库驱动的应用程序和数据库orm的知识是wee,我用peewee编写了这个模型https://codereview.stackexchange.com/q/210293/22943
我希望能够用patient table中patient id的relative和patientattendovisit外键同时更新patient、relative和patientattenovisit三个表。
我试着直截了当地说:

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"]
            )

但尝试这样做会给我以下错误:
return controller.add_patient_visit(data=data)file“/users/ciasto/development/python/clinic backend/app/api/clinic/controller.py”,第35行,add_patient_visit relativename=data[“relativename”])file“/users/ciasto/development/python/clinic backend/clinic_venv/lib/python2.7/site packages/peewee.py”,第5580行,在create inst.save(force\u insert=true)file“/users/ciasto/development/python/clinic backend/clinic\u venv/lib/python2.7/site packages/peewee.py”行5727中,在save pk\u from\u cursor=self.insert(**field\u dict).execute()file“/users/ciasto/development/python/clinic backend/clinic\u venv/lib/python2.7/site packages/peewee.py”行,在第1693行的“内部返回方法(self,database,*args,**kwargs)文件”/users/ciasto/development/python/clinic backend/clinic\u venv/lib/python2.7/site packages/peewee.py”中,在第2355行的“执行返回方法(self,database,*args,**kwargs)文件”/users/ciasto/development/python/clinic backend/clinic\u venv/lib/python2.7/site packages/peew,在第2118行的“execute return super(insert,self)。\u execute(database)file”/users/ciasto/development/python/clinic backend/clinic\u venv/lib/python2.7/site packages/peewee.py”中,在第2724行的“execute cursor=database.execute(self)file”/users/ciasto/development/python/clinic backend/clinic\u venv/lib/python2.7/site packages,在execute return self.execute \u sql(sql,params,commit=commit)file“/users/ciasto/development/python/clinic backend/clinic \u venv/lib/python2.7/site packages/peewee.py”行2718中,在execute \u sql self.commit()file“/users/ciasto/development/python/clinic backend/clinic \u venv/lib/python2.7/site packages/peewee.py,在execute\ sql cursor.execute(sql,params或())file“/users/ciasto/development/python/clinic backend/clinic\ venv/lib/python2.7/site packages/peewee.py”第2711行的exit\ sql cursor.execute(sql,params或())file“/users/ciasto/development/python/clinic backend/clinic\ venv/lib/python2.7/site packages/mysqldb/cursors.py”第205行中,在execute self.errorhandler(self,exc,value)file“/users/ciasto/development/python/clinic backend/clinic\u venv/lib/python2.7/site packages/mysqldb/connections.py”的第36行,在defaulterrorhandler raise errorclass中,errorvalue integrityerror:(1452,'无法添加或更新子行:外键约束失败( clinic_backend . relative ,约束 relative_ibfk_1 外键( patient_id )参考文献 patient ( id ))')

yizd12fk

yizd12fk1#

没什么复杂的,我想,

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"]
            )

相关问题