sequelize: Cannot re-define a new message for unique attribute.

module.exports = function (sequelize, DataTypes) {
    var users = sequelize.define('users', {
        full_name: {
            type: DataTypes.STRING,
            allowNull: false,
            validate: {
                len: {
                    args: [5, 50],
                    msg: 'Your full name may be 5 to 50 characters only.'
                }
            }
        },
        email: {
            type: DataTypes.STRING,
            allowNull: false,
            unique: {
                msg: 'This email is already taken.'
            },
            validate: {
                isEmail: {
                    msg: 'Email address must be valid.'
                }
            }
        },
        username: {
            type: DataTypes.STRING,
            allowNull: false,
            unique: {
                msg: 'This username is already taken.'
            },
            validate: {
                len: {
                    args: [5, 50],
                    msg: 'Your username may be 5 to 50 characters only.'
                }
            }
        },
        password: {
            type: DataTypes.STRING,
            allowNull: false,
            validate: {
                len: {
                    args: [5, 72],
                    msg: 'Your password may be 5 to 72 characters only.'
                }
            }
        },
        rank: {
            type: DataTypes.INTEGER,
            allowNull: false,
            validate: {
                isInt: true
            }
        }
    }, {
        hooks: {
            beforeValidate: function (user, options) {
                if (typeof user.email === 'string') {
                    user.email = user.email.toLowerCase();
                }

                if (typeof user.username === 'string') {
                    user.username = user.username.toLowerCase();
                }
            }
        }
    });

    return users;
};

As you can see, the email and username fields have two different messages set for unique attribute. However, whenever I enter a new email but an already existing username, I get this error:

{
  "name": "SequelizeUniqueConstraintError",
  "message": "Validation error",
  "errors": [
    {
      "message": "This email is already taken.",
      "type": "unique violation",
      "path": "username",
      "value": "hassan"
    }
  ],
  "fields": {
    "username": "hassan"
  }
}

So as you can see, it says it is the username that is not unique, but uses the message defined for the email property.

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 1
  • Comments: 35 (9 by maintainers)

Most upvoted comments

Try it like this instead:

email: {
  type: DataTypes.STRING,
  allowNull: false,
  unique: {
    msg: 'This email is already taken.',
    fields: ['email']
  }
},
username: {
  type: DataTypes.STRING,
  allowNull: false,
  unique: {
    msg: 'This username is already taken.',
    fields: ['username']
  }
}

Me too! Custom message dose not work when creating composite unique index !

Try this, this worked for me

    email: {
      allowNull: false,
      unique: {
        name: 'email',
        msg: 'Oops. Looks like you already have an account with this email address. Please try to login.',
      },
    }

@mhmdtshref I don’t use this ORM anymore because of so many problems that came up. You can try referring to the docs for updates or open up a new issue with your specific code.

@mickhansen I gave up on that completely. Now I am stuck with this stupid allowNull: {} check. In that, I can’t specify a message. 😕 It gives an ugly message. If I disable it, other validations don’t work. Any fix for that?

full_name: {
            type: DataTypes.STRING,
            allowNull: {
                args: false,
                msg: 'The full name field cannot be empty!'
            },
            validate: {
                notNull: {
                    args: true,
                    msg: 'Your full name field cannot be empty.'
                },
                len: {
                    args: [5, 50],
                    msg: 'Your full name may be 5 to 50 characters only.'
                }
            }
        },