'use strict';

var bcrypt = require('bcrypt');

module.exports = function(sequelize, DataTypes) {
    var User = sequelize.define("User", {
        username: DataTypes.STRING,
        email: {
            type: DataTypes.STRING,
            allowNull: false,
            validate: {
                isEmail: true
            }
        },
        password_digest: {
            type: DataTypes.STRING,
            validate: {
                notEmpty: true
            }
        },
        password: {
            type: DataTypes.VIRTUAL,
            allowNull: false,
            validate: {
                notEmpty: true
            }
        },
        password_confirmation: {
            type: DataTypes.VIRTUAL
        },
        school: DataTypes.STRING,
        sex: {  // false -> male, true -> female
            type: DataTypes.BOOLEAN,
            allowNull: false,
            defaultValue: false
        },
        interestedIn: DataTypes.BOOLEAN, // null -> both, false -> male, true -> female
        isChatting: {
            type: DataTypes.BOOLEAN,
            allowNull: false,
            defaultValue: false
        }
    }, {
        indexes: [
            {unique: true, fields: ['email']}
        ],
        instanceMethods: {
            authenticate: function(value) {
                if (bcrypt.compareSync(value, this.password_digest))
                    return this;
                else
                    return false;
            }
        }
    }, {
        classMethods: {
            associate: (models) => {
                User.belongsTo(models.University, {
                    onDelete: "CASCADE",
                    foreignkey: {
                        allowNull: false
                    }
                });
            }
        }
    });

    const hasSecurePassword = (user, options, callback) => {
        if (user.password != user.password_confirmation) {
            throw new sequelize.ValidationError("Password confirmation: Password confirmation doesn't match Password");
        }
        if (user.password.length < 6) {
            throw new sequelize.ValidationError("Password should be at least 6 characters");
        }
        bcrypt.hash(user.get('password'), 10, function(err, hash) {
            if (err) return callback(err);
            user.set('password_digest', hash);
            return callback(null, options);
        });
    };

    User.beforeCreate(function(user, options, callback) {
        user.email = user.email.toLowerCase();
        if (user.password)
            hasSecurePassword(user, options, callback);
        else
            return callback(null, options);
    });

    User.beforeUpdate(function(user, options, callback) {
        user.email = user.email.toLowerCase();
        if (user.password)
            hasSecurePassword(user, options, callback);
        else
            return callback(null, options);
    });

    return User;
};

@@ -1465,33 +1465,25 @@
  %7D%0A        %7D
-%0A    %7D, %7B
+,
 %0A        cla
@@ -1528,24 +1528,24 @@
 odels) =%3E %7B%0A
-
             
@@ -1738,24 +1738,230 @@
         %7D);%0A
+                User.belongsTo(models.School, %7B%0A                    onDelete: %22CASCADE%22,%0A                    foreignkey: %7B%0A                        allowNull: false%0A                    %7D%0A                %7D);%0A
             

Add belongsTo association for User to School