Sails.js 复合唯一字段

Sails.js 复合唯一字段

问题描述:

这个模型以重复数据和一般丑陋为代价给了我想要的效果:

This model gives me the effect I want at the expense of duplicated data and general uglyness:

//Example
var Example = {
    attributes: {
        foo: { 
            type: 'string',
            required: true
        },
        bar: {
            type: 'string',
            required: true,
        },
        baz: {
            type: 'integer',
            required: true,
            min: 1
        },
        foobar: {
            type: 'string',
            unique: true
        }
    },
    beforeValidation : function(values,cb) {
        values.foobar = values.foo+values.bar;
        cb();
    }
};
module.exports = Example;

是否有更好的策略来创建复合唯一键?

Is there a better strategy for creating a composite unique key?

在sails 中没有办法直接做到这一点,参见https://github.com/balderdashy/waterline/issues/221.最好的解决方案是直接在您计划使用的数据库中执行此操作.

There's no way to do this directly in sails see https://github.com/balderdashy/waterline/issues/221. The best solution is to do this directly in the db you plan to use.