0

我有两个模型:

PdfAnnotation.js:

module.exports = {
  tableName: "pdf_annotations",
  primaryKey: "pk_id",

  attributes: {
    pk_id: {
      type: "number",
      autoIncrement: true
    },
    annotation_id: {
      type: "string",
      unique: true,
      required: true,
    },
    comments: {
      collection: "pdfcomments",
      via: "fk_annotation_id"
    }
  }
};

PdfComments.js:


module.exports = {
  tableName: "pdf_comments",
  primaryKey: "pk_id",

  attributes: {
    pk_id: {
      type: "number",
      autoIncrement: true,
    },
    fk_annotation_id: {
      model: "pdfannotations",
    },
    comment_content: {
      type: "string",
    },
  }
};

当我运行这些代码时:

PdfAnnotations.create({
  annotation_id: "test3",
});
PdfComments.create({
  fk_annotation_id: 'test3',
  comment_content: 'test',
});

我收到了这个错误:

在此处输入图像描述

我遵循了文档:https ://sailsjs.com/documentation/concepts/models-and-orm/associations/one-to-many 。我的实现和文档之间的区别是:我通过唯一字段annotation_id(string)而不是主键pk_id(number)PdfComments用于PdfAnnotations的约束,所以我得到了错误。

由于某些原因我不想使用 annotation_id 作为主键(例如它的类型是字符串)

我对 Sails 及其 ORM 不熟悉,希望得到您的帮助。

4

1 回答 1

0

尝试这样的事情:

const pdfannotation = await PdfAnnotations.create({
  annotation_id: 'test3',
}).fetch();

const pdfcomment = await PdfComments.create({
  fk_annotation_id: pdfannotation.id,
  comment_content: 'test',
});
于 2020-05-31T13:37:27.503 回答