Uniqueness constraint for multiple attributes with WaterlineJS

I created the following join table to link users and game model.

I would like to create a unique constraint (user, foot).

Here is my code:

module.exports = {

  attributes: {
    foot:{
        type: 'integer',
        foreignKey: true,
        references: 'foot',
        on: 'id'
    },
    user:{
        type: 'integer',
        foreignKey: true,
        references: 'user',
        on: 'id'        
    },
    statut:{
        type: 'integer', // 0 invited, 1 playing, 2 denied, 3 organisator
        defaultsTo: 0
    }
  }
};

      

I am using the "sails-mysql" adapter.

Does the waterline support this or should I do it directly in my DB?

Thank!

+3


source to share


1 answer


I haven't done anything like this before. Your problem seemed interesting to me. So I did a little work. There are several issues in the github repository of the waterline to solve this problem. One of them is number # 244 and the other is number # 221 . None of them have any indication of a solid solution to your problem, and I think it is Waterline

impossible to do with an ORM

But since you are using mysql, you can do this by executing a raw SQL query from your database adapter. In, config/bootstrap.js

you can write a script to check if this constraint is available in your database. If it is available, you do nothing, but if it is not available, you create a new constraint to enforce uniqueness.



I think this is not the solution you were looking for, but I am afraid this is the best solution you have at the moment. Hope it helps.

+5


source







All Articles