【问题标题】:extra columns in model association sailsjs模型关联sailsjs中的额外列
【发布时间】:2018-05-20 15:00:18
【问题描述】:

如何在 postgres 中添加一个带有sailsjs 模型关联的额外列?

这是我的两个模型的示例

// Users.js attribute
...
challenges: {
  collection: 'challenge',
  via: 'userChallenge'
}


// Challenge.js attribute
...
userChallenge: {
  collection: 'users',
  via: 'challenges'
}
...

有了这个我得到了表关联(多对多)

 id | challenge_userChallenge | users_challenges 

我需要一个或多个额外的列,例如“活动”或类似的内容

提前致谢

【问题讨论】:

  • 您不能在模型中添加新列吗?目前尚不清楚您打算通过添加其他列来做什么。您是否尝试为关系创建复合键?
  • @E.Celis 我的意图是在多对多关系表中增加一列

标签: sails.js sails-postgresql


【解决方案1】:

你应该使用through associations

多对多通过关联的行为方式与多对多相同 除了自动连接表之外的关联 为您创建。在多对多直通关联中,您定义了一个 模型包含对应于您将要使用的两个模型的两个字段 一起加入。定义关联时,您将添加 通过 key 表明应该使用模型而不是 自动连接表。

我们以PostTag 模型为例。 Post 拥有并属于许多 TagTag 拥有并属于许多 Post。这两个模型将通过PostTag 模型加入。

我们的Post 模特:

/**
 * Post.js
 *
 * @description :: A model definition.  Represents a database table/collection/etc.
 * @docs        :: https://sailsjs.com/docs/concepts/models-and-orm/models
 */

module.exports = {

  tableName: 'posts',

  attributes: {

    name: {
      type: 'string',
      required: true
    },

    // Many to many: Post has and belongs to many Tag.
    tags: {
      collection: 'Tag',
      via: 'postId',
      through: 'PostTag'
    }

};

我们的Tag 模特:

/**
 * Tag.js
 *
 * @description :: A model definition.  Represents a database table/collection/etc.
 * @docs        :: https://sailsjs.com/docs/concepts/models-and-orm/models
 */

module.exports = {

  tableName: 'tags',

  attributes: {

    name: {
      type: 'string',
      unique: true,
      required: true
    },

    // Many to many: Tag has and belongs to many Post.
    posts: {
      collection: 'Post',
      via: 'tagId',
      through: 'PostTag'
    }

  }

};

我们的PostTag 模型(我们手动创建它,我们不希望 Sails.js 自动创建它):

/**
 * PostTag.js
 *
 * @description :: A model definition.  Represents a database table/collection/etc.
 * @docs        :: https://sailsjs.com/docs/concepts/models-and-orm/models
 */

module.exports = {

  tableName: 'posts_tags',

  attributes: {

    postId: {
      model: 'Post'
    },

    tagId: {
      model: 'Tag'
    }

  }

};

PostTag 模型实际上是连接表。在此模型中,您可以定义额外的字段。

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    虽然 Vladyslav Turak 的 answer 适用于 Sails v1.0 及更高版本,但请注意 Through AssociationsSails 0.12 中不支持

    要实现与 Sails 0.12 类似的效果,您可以使用以下内容:

    Post 模型:

    /**
     * Post.js
     *
     * @description :: A model definition.  Represents a database table/collection/etc.
     * @docs        :: https://sailsjs.com/docs/concepts/models-and-orm/models
     */
    
    module.exports = {
    
      attributes: {
    
        name: {
          type: 'string',
          required: true
        },
    
        // Many to many: Post has and belongs to many PostTag.
        tags: {
          collection: 'PostTag',
          via: 'post'
        }
    
    };
    

    Tag 模型:

    /**
     * Tag.js
     *
     * @description :: A model definition.  Represents a database table/collection/etc.
     * @docs        :: https://sailsjs.com/docs/concepts/models-and-orm/models
     */
    
    module.exports = {
    
      attributes: {
    
        name: {
          type: 'string',
          unique: true,
          required: true
        },
    
        // Many to many: Tag has and belongs to many PostTag.
        posts: {
          collection: 'PostTag',
          via: 'tag',
        }
    
      }
    
    };
    

    我们的PostTag 模型(我们手动创建它,我们不希望 Sails.js 自动创建它):

    /**
     * PostTag.js
     *
     * @description :: A model definition.  Represents a database table/collection/etc.
     * @docs        :: https://sailsjs.com/docs/concepts/models-and-orm/models
     */
    
    module.exports = {
    
      attributes: {
    
        post: {
          model: 'Post'
        },
    
        tag: {
          model: 'Tag'
        },
    
        customField: {
          type: 'string'
        }
    
      }
    
    };
    

    PostTag 模型实际上是连接表。在此模型中,您可以定义额外的字段。

    希望这对使用 Sails v0.12 的人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-23
      • 1970-01-01
      • 2015-12-24
      • 1970-01-01
      • 2013-07-22
      • 1970-01-01
      • 1970-01-01
      • 2019-01-13
      相关资源
      最近更新 更多