【问题标题】:Many-to-many relationship in sails帆中的多对多关系
【发布时间】:2013-10-03 09:55:37
【问题描述】:

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

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

【问题讨论】:

    标签: node.js sails.js


    【解决方案1】:

    如果切换到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 中的代码才能了解发生了什么。

    【讨论】:

    • 我不断收到TypeError: Cannot set property 'junctionTable' of undefined 这个。
    • 我明白了!只是要注意做小写的一切(gist.github.com/clouddueling/70cee9b67077bd93b228
    【解决方案2】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2014-11-27
      • 1970-01-01
      • 2018-06-15
      • 2011-06-02
      • 2011-02-07
      • 2015-12-30
      • 2020-12-05
      • 2018-09-28
      • 2017-05-22
      相关资源
      最近更新 更多