0

js. I want to insert data by objection transaction.

I started transaction with PrescriptionMetaData Model and related PrescriptionSymptom Model by relationMappings as followings.

Eventually can't able to insert/patch with the following error, PrescriptionMetaData.relationMappings.symptoms: join: either from or to must point to the owner model table and the other one to the related table. It might be that specified table 'prescription_symptoms' is not correct

Could anyone please help me out.

const prescriptionMetaDataTrx = await PrescriptionMetaData.startTransaction();

const metaData = await PrescriptionMetaData.upsertOne(
        {
          appointmentId: id,
          appointmentTS: startConsultationTS,
          mpid,
          amsDoctorId: objectId,
          mobileNumber: userMobile,
          email: userEmail,
          followupDate,
        },
        prescriptionMetaDataTrx
      );


const data: Partial<PrescriptionSymptom> = {
      ...symptomData,
      version: symptomDoc.version,
      name: symptomDoc.name,
      speciality: symptomDoc.speciality,
      aliases: symptomDoc.aliases,
      appointmentId: metaData.appointmentId,
      appointmentTS: metaData.appointmentTS,
      amsDoctorId: metaData.amsDoctorId,
      mpid: metaData.mpid,
      prescriptionId: metaData.id,
      options: JSON.stringify(options),
    };


const prescriptionSymptom = PrescriptionMetaData.upsertOneSymptom(metaData, data)

Inside PrescriptionMetaData DAO

static get tableName() {
    return "prescription_meta_data";
  }

static get relationMappings() {
    return {
      symptoms: {
        relation: Model.HasManyRelation,
        modelClass: PrescriptionMetaData,
        join: {
          from: `${PrescriptionMetaData.tableName}.id`,
          to: `${PrescriptionSymptom.tableName}.prescriptionId`,
        },
      },
    };
  }

static async upsertOne(
    metaData: Partial<PrescriptionMetaData>,
    trx: Knex.Transaction
  ) {
    // check for the doc already created
    const prescriptionMetaDataDoc = await PrescriptionMetaData.query().findOne({
      mpid: metaData.mpid,
      appointmentId: metaData.appointmentId,
    });

    if (prescriptionMetaDataDoc) {
      // patch operation
      return PrescriptionMetaData.query(trx).patchAndFetchById(
        prescriptionMetaDataDoc.id,
        {
          ...metaData,
          mpid: prescriptionMetaDataDoc.mpid,
          appointmentId: prescriptionMetaDataDoc.appointmentId,
        }
      );
    }
    // insert operation
    return PrescriptionMetaData.query(trx).insert(metaData);
  }

static async upsertOneSymptom(
    trxObject: PrescriptionMetaData,
    symptomData: Partial<PrescriptionSymptom>
  ) {
    // check for the doc already created
    const prescriptionSymptomDoc = await PrescriptionSymptom.query().findOne({
      symptomId: symptomData.symptomId,
      appointmentId: symptomData.appointmentId,
      prescriptionId: symptomData.prescriptionId,
    });

    if (!prescriptionSymptomDoc) {
      // insert operation
      const newDoc = await trxObject
        .$relatedQuery<PrescriptionSymptom>("symptoms")
        .insert(symptomData);
      return newDoc;
    }

    // patch operation
    const updatedDoc = await trxObject
      .$relatedQuery<PrescriptionSymptom>("symptoms")
      .patchAndFetchById(prescriptionSymptomDoc.id, symptomData);
    return updatedDoc;
  }
Harik
  • 13
  • 3

0 Answers0