模型关联 sailsjs 中的额外列

extra columns in model association sailsjs

我怎样才能在带有 sailsjs 模型关联的 postgres 中有一个额外的列?

这是我的两个模型的例子

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


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

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

 id | challenge_userChallenge | users_challenges 

我需要一个或多个额外的列,例如 "active" 或类似的列

提前致谢

你应该使用 through associations.

Many-to-Many through associations behave the same way as many-to-many associations with the exception of the join table being automatically created for you. In a Many-To-Many through assocation you define a model containing two fields that correspond to the two models you will be joining together. When defining an association you will add the through key to show that the model should be used rather than the automatic join table.

我们以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模型其实就是jointable。在此模型中,您可以定义额外的字段。

希望这对您有所帮助。

虽然 by Vladyslav Turak is correct for Sails v1.0 and up, please note that Through Associations 不支持 Sails 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模型其实就是jointable。在此模型中,您可以定义额外的字段。

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