【问题标题】:Achieve a transitive association for a query in SailsJS在 SailsJS 中实现查询的传递关联
【发布时间】:2021-01-06 14:12:32
【问题描述】:

我的问题可能很明显,但是我已经为此苦苦挣扎了几个晚上。这是我的问题的基本概述。

我有以下三种型号:

模型 1

/* api/models/Parent.js */

module.exports = {
  tableName: "table1",
  attributes: {
    name: {
      type: "string",
    },
    children: {
      collection: "child",
      via: "parentId"
    },
  },
};

模型 2

/* api/models/Child.js */

module.exports = {
  tableName: "table2",
  attributes: {
    name: {
      type: "string",
    },
    parentId: {
      model: "parent"
    },
    vaccinations: {
      collection: "vaccination",
      via: "childId"
    },
  },
};

模型 3

/* api/models/Result.js */

module.exports = {
  tableName: "table3",
  attributes: {
    name: {
      type: "string",
    },
    childId: {
      model: "child"
    },
    vaccinationId: {
      model: "vaccination"
    },
  },
};

我希望能够查询以下内容 GET /vaccination?parentId=1 将检索具有parentId=1 的儿童的所有疫苗接种。我不确定在这里遵循的最佳做法是什么,我非常感谢任何帮助。我已经阅读了Through Associations,但由于某种原因,我无法让它工作。

提前致谢。

【问题讨论】:

    标签: database join sails.js associations


    【解决方案1】:

    以下查询可能会对您有所帮助:

    let children = await Child.find({
      parentId: 1
    })
    .populate('parentId')
    .populate('vaccinations');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多