帆中的多对多关系

时间:2013-10-03 09:55:38

标签: node.js sails.js

在我的风帆应用程序(真的是风帆新手)中,我有3个模型:
- 用户(电子邮件,密码)
- 团体(姓名)
- 项目(名称)
我希望群组模型充当用户和项目之间的多对多关系,如何实现?

我正在使用风帆0.9.4,我已经阅读了关于这种关系的问题#124但是并不真正理解这是否可以应用于现有模型(在itemId和userId之上也包含它自己的属性) )。

2 个答案:

答案 0 :(得分:7)

如果切换到v0.10分支,可以尝试以下操作,

// Users.js
module.exports = {
    tableName: 'user',
    attributes: {
        email: 'STRING',
        password: 'STRING',
    }
    items: {
        collection: 'item',
        via: 'users',
        through: 'useritem'
    }
}

// Items.js
module.exports = {
    tableName:'item',
    attributes: {
        name: 'STRING'
    }
    users: {
        collection: 'user',
        via: 'items',
        through: 'useritem'
    }
}

// Groups.js
module.exports = {
  tableName: 'group',
  tables: ['user', 'item'],
  junctionTable: true,

  attributes: {
    id: {
      primaryKey: true,
      autoIncrement: true,
      type: 'integer'
    },
    user_items: {
      columnName: 'user_items',
      type: 'integer',
      foreignKey: true,
      references: 'user',
      on: 'id',
      via: 'item_users',
      groupBy: 'user'
    },
    item_users: {
      columnName: 'item_users',
      type: 'integer',
      foreignKey: true,
      references: 'item',
      on: 'id',
      via: 'user_items',
      groupBy: 'item'
    }
  }
}

我必须浏览this file中的代码才能知道发生了什么。

答案 1 :(得分:1)

从Sails.js v0.9.4开始,该框架尚不支持关联。

Balderdash(Sails的开发人员)表示,协会和交易都在生产版本的路线图上,并在GitHub上的#124 issue中记录了可能的API

目前有一个development branch的Waterline(Sails使用的ORM适配器)支持关联,但是在查找新API的文档时可能会遇到一些问题

目前,您必须具有创造力,并寻找其他方式来连接您的用户和项目

相关问题