Skip to content

Not able to insert with transaction. #2331

Closed

Description

@Harikrishnan19

Hi I am new to objection.js. I want to insert data by transaction.

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

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 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;
  }

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.

Activity

  1. locked and limited conversation to collaborators on Jul 5, 2023
  2. converted this issue into a discussion #2457 on Jul 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions