Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sails.js composite unique field

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?

like image 915
user2458080 Avatar asked Jul 24 '14 01:07

user2458080


1 Answers

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.

like image 173
JohnGalt Avatar answered Sep 21 '22 14:09

JohnGalt