sequelize-auto icon indicating copy to clipboard operation
sequelize-auto copied to clipboard

Creating with Associations TypeScript

Open darkMark1999 opened this issue 3 years ago • 0 comments

If we want to create a record using an associative query, then we are faced with the problem that the attribute field of the associated model is not described in the model interface, and therefore TS issues a typing error.

I solved this problem by adding an attribute field to the associated moldel.

Perhaps I'm somehow using the models incorrectly.

Auto generation interface:

export interface status_mergesAttributes {
  status_id: number;
  fk_merge_id: number;
  fk_responsible_id: number;
  status: string;
  create_at?: Date;
}

Updated interface:

export interface status_mergesAttributes {
  status_id: number;
  fk_merge_id: number;
  fk_responsible_id: number;
  status: string;
  create_at?: Date;
  fk_merge?: merge_requestsCreationAttributes;
}
}

Code:

await status_merges.create(
  {
    fk_responsible_id: merge.authorId,
    status: "PENDING",
    fk_merge: {
      merge_segment_from: merge.segmentFrom,
      merge_segment_to: merge.segmentTo,
      fk_author_id: merge.authorId,
      merge_sha: merge.sha,
      fk_project_id: merge.projectId,
    },
  },
  {
    include: [
      {
        model: merge_requests,
        as: "fk_merge",
      },
    ],
    transaction: transaction,
  }
);

darkMark1999 avatar Jul 19 '22 11:07 darkMark1999