balderdashy / sails

Realtime MVC Framework for Node.js
https://sailsjs.com
MIT License
22.85k stars 1.95k forks source link

unique check is not working #4605

Open sayjeyhi opened 5 years ago

sayjeyhi commented 5 years ago

Waterline version: v0.13 Node version: 10.0.7 NPM version:6.1.0 Operating system: window8


I am using sails v1.2.1 with waterline , with sails-mongo and set an attribute unique but I got multiple rows !

/**
 * User.js
 *
 * @description :: A model definition represents a database table/collection.
 * @docs        :: https://sailsjs.com/docs/concepts/models-and-orm/models
 */

const bcrypt = require('bcrypt-nodejs');

module.exports = {
    attributes: {
        email: {
            type    : 'string',
            required: true,
            unique  : true
        },
        username: {
            type    : 'string',
            required: true,
            unique  : true
        },
        firstName: {
            type: 'string'
        },
        lastName: {
            type: 'string'
        },
        password: {
            type    : 'string',
            required: true
        },
        mobile: {
            type: 'string'
        },
        isOwner: {
            type      : 'boolean',
            defaultsTo: false
        },
        avatar: {
            type: 'string'
        },
        items: {
            collection: 'item',
            via       : 'members'
        },
        apps: {
            collection: 'app',
            via       : 'users'
        }
    },

    customToJSON() {
        return _.omit(this, ['password']);
    },
    beforeCreate(user, cb) {
        bcrypt.genSalt(10, (err, salt) => {
            bcrypt.hash(user.password, salt, null, (err, hash) => {
                if (err) return cb(err);
                user.password = hash;
                return cb();
            });
        });
    }
};
sailsbot commented 5 years ago

Hi @jafar-rezaei! It looks like you missed a step or two when you created your issue. Please edit your comment (use the pencil icon at the top-right corner of the comment box) and fix the following:

As soon as those items are rectified, post a new comment (e.g. “Ok, fixed!”) below and we'll take a look. Thanks!

*If you feel this message is in error, or you want to debate the merits of my existence (sniffle), please contact inquiries@sailsjs.com

sayjeyhi commented 5 years ago

Ok, fixed!

sailsbot commented 5 years ago

@jafar-rezaei Thanks for posting, we'll take a look as soon as possible.


For help with questions about Sails, click here. If you’re interested in hiring @sailsbot and her minions in Austin, click here.

sayjeyhi commented 5 years ago

It was because of models config migrate , and mongodb user access to create indexes (we add role to readWrite should create dbOwner) , after set migrate:'alter' and set user access the error fixed.

rachaelshaw commented 5 years ago

@jafar-rezaei just a heads up, we're attempting to consolidate issues into one place now, so I've transferred this one into the sails repo. Are you still experiencing this issue with uniqueness checks, or did those changes solve it?

sayjeyhi commented 5 years ago

@rachaelshaw thanks , actually I still have it , it did not create index on mongodb when we use mongo authentication , without authentication it seems to works fine ... 😞

german970814 commented 5 years ago

I have the same behaviour on with sails-mysql. Validation is not performed

sayjeyhi commented 5 years ago

@german970814 I disappointed from auto creation unique index and add it on app bootstrap

raqem commented 5 years ago

Hi @german970814 and @jafar-rezaei I want to look into this issue some more. Can you both please provide your Socket and hook version information so that I can start gathering enough info to try to reproduce the issue.

german970814 commented 5 years ago

My dependences:

"@sailshq/lodash": "3.10.3",
"bcryptjs": "2.4.3",
"lodash": "4.17.11",
"sails": "1.1.0",
"sails-hook-orm": "2.1.1",
"sails-hook-sockets": "1.5.5",
"sails-mysql": "1.0.1"

I'm getting the issue on action validation

raqem commented 5 years ago

I did some more digging around and found some information that we may have over looked. As the Docs say: setting unique: true is not really a validation it is a "database-level constraint." Please read more of this section of the docs for an example and further explanation.