SailsJS -水线-添加列到多对多关系



我试图在以下对象之间创建一个多对多关系,我想包含一个"is_active"关系的布尔列。

对象如下:

Item.JS

module.exports = {
attributes: {
name: {
type: 'string'
},
description: {
type: 'string',
allowNull: true
},
categories: {
collection: 'category',
via: 'items',
},
}
}

Category.JS

module.exports = {
attributes: {
name: {
type: 'string'
},
description: {
type: 'string',
allowNull: true
},
items: {
collection: 'item',
via: 'categories',
},
}
}

如何添加"is_active"布尔列和查询由它和顺序表示与项目和类别之间的关系是活跃的吗?

谢谢!

你要找的是"到"模型。实际上,现在,您正在使用自动生成的模型(类似于"item_categories")。或"categories_item"。但是,如果你需要了解一段关系的更多数据,你会想要创建一个"贯穿"模型,您可以在其中添加您可能需要的任何其他数据。

请参阅此处的Sails.js文档,了解更多关于through关联的信息。

根据目前的文档,这样做的缺点是:

目前,如果您想向through表添加额外的信息,那么在调用.populate时将不可用。要做到这一点,您需要手动查询through模型。

因此,您必须手动查询ItemCategory模型以确定它是否处于活动状态。

但是,这是关于它的样子:

Item.js

module.exports = {
attributes: {
name: {
type: 'string'
},
description: {
type: 'string',
allowNull: true
},
categories: {
collection: 'category',
via: 'item',
through: 'itemcategory'
}
}
};

Categories.js

module.exports = {
attributes: {
name: {
type: 'string'
},
description: {
type: 'string',
allowNull: true
},
categories: {
collection: 'item',
via: 'category',
through: 'itemcategory'
}
}
};

ItemCategory.js

module.exports = {
attributes: {
item: {
model: 'item'
},
category: {
model: 'category'
},
isActive: {
type: 'bool'
}
}
};