Sails中的模型的独特属性

我是SAILS的新手。 我有以下型号。

型号\ Company.js

module.exports = { attributes: { name: { type: 'string', required: true, unique: true }, description: { type: 'string', required: true } } }; 

型号\ Project.js

 module.exports = { attributes: { name: { type: 'string', required: true }, key: { type: 'string', required: true, unique: true }, description: { type: 'string' }, company: { type: 'string', required: true }, startedDateTime: { type: 'datetime' }, completedDateTime: { type: 'datetime' }, members: { collection: 'ProjectMember', via: 'project', dominant: true } }; 

我需要这样一个模型,即可以有多个公司,但是项目对于一个特定的公司来说必须是独一无二的,但是在两个不同的公司中它可以是相同的。 我如何修改我的模型来获取?

sails中没有这样的configuration选项。

但是你可以使用生命周期callback手动处理。 您可以添加检查beforeValidate和afterValidate函数。

看看这里: http : //sailsjs.org/#/documentation/concepts/ORM/Lifecyclecallbacks.html

 afterValidate: function(values, cb) { var params = {company: values.company}; if (values._id) { params['_id'] = {$not: values._id}; } Project.find(params).exec(function(err, projects) { if (projects.length > 0) return cb(new Error('Project already binded')); cb(); }); } 

这只是样品,所以它可能无法正常工作,请检查条件和事件。