首页 文章

帆中的多对多关系

提问于
浏览
5

在我的帆应用程序(真的是帆船的新手)中,我有3个型号:

  • 用户(电子邮件,密码)
  • 团体(姓名)
  • 物品(名称)
    我希望Groups模型能够充当用户和项目之间的多对多关系,如何实现这一目标?

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

2 回答

  • 1

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

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

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

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

  • 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中的代码来了解发生了什么 .

相关问题