【问题标题】:Models associations not working on Waterline模型协会不适用于 Waterline
【发布时间】:2016-05-22 10:59:25
【问题描述】:

我正在使用Dogwater 包将Waterline ORM 集成到一个基于Hapi 的NodeJS 项目中。

我无法使用简单的一对多关联(基础数据库是 Mongo)。

所有者模型如下:

  identity: 'companies',
  tableName: 'companies',

  connection: 'viveatDashboard',
  schema: true,
  attributes: {
    _id: {
      type: 'string',
      primaryKey: true,
      required: true,
      unique: true
    },
    apps: {
      collection: 'apps',
      via: 'companyId'
    },
    ... // more attributes here
    migrate: 'safe',
    autoCreatedAt: true,
    autoUpdatedAt: true,

引用公司的模型如下所示:

  identity: 'apps',
  tableName: 'apps',

  connection: 'viveatDashboard',
  schema: true,

  _id: {
    type: 'string',
    primaryKey: true,
    required: true,
    unique: true
  },

  companyId: {
    type: 'string',
    required: true,
    model: 'companies'
  },
  ... // more attributes here
  migrate: 'safe',
  autoCreatedAt: true,
  autoUpdatedAt: true,

当路由处理程序的代码运行时:

...
Companies.find().populate('apps').exec(function(err, apps) {
  return reply(apps);
});

获取记录的apps 属性是一个空数组(我确保确实存在引用现有公司的应用程序)。

【问题讨论】:

  • 尝试运行sails lift --silly 并触发处理程序。日志应该提供一些对调试有用的额外信息

标签: node.js mongodb sails.js waterline hapijs


【解决方案1】:

显然问题在于,如果将_id 属性用作primaryKey,则必须在模型架构中将其引用为id,而不是_id
现在关联正常工作了。

【讨论】:

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