SailsJs / Postgresql-如何通过唯一字段创建单向关联或一对多关系

问题描述 投票:0回答:1

我有两种型号:

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',
});

我收到此错误:

enter image description here

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

出于某些原因,我不想使用注解ID作为主键(例如其类型为字符串)] >>

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

[我有两个模型:PdfAnnotation.js:module.exports = {tableName:“ pdf_annotations”,primaryKey:“ pk_id”,属性:{pk_id:{类型:“ number”,autoIncrement:true},...

sails.js waterline sails-postgresql
1个回答
0
投票

尝试这样的事情:

© www.soinside.com 2019 - 2024. All rights reserved.