Wednesday 26 April 2017

users is not associated to clients error in Model.validateIncludedElement

I am using sequilize js. while joining table I am getting an error that table is not associated. Code snippet is given below

Error: Unhandled rejection Error: tbl3_users is not associated to tbl4_clients! at Model.validateIncludedElement.

"use strict";
// User Model
    var bcrypt = require('bcrypt');
    var moment = require('moment');
    var DB = require('../db');

    const saltRounds = 10;

    module.exports = function (sequelize, DataTypes) {
        var User = sequelize.define("tbl3_users", {
            i_company_id: DataTypes.INTEGER(11),
            v_first_name: DataTypes.STRING(100),
            v_last_name: DataTypes.STRING(100),
            v_email: {
                type: DataTypes.STRING(100),
                unique: true
            },
            password: DataTypes.STRING(200),
            v_phone: DataTypes.STRING(100),
            v_profile_img: DataTypes.STRING(200),
            e_type: DataTypes.ENUM('Company Contact', 'Client'),
            i_client_id: DataTypes.INTEGER(11),
            e_company_contact_type: DataTypes.ENUM('Primary', 'Secondary'),
            i_last_modified_by: DataTypes.INTEGER(11),
            remember_token: DataTypes.STRING(100)
        }, 
        {
            classMethods: {
                associate: function (DB) {
                    User.hasMany(DB.Client, { foreignKey: 'i_user_id' });
                }
            }
        },
        {
            freezeTableName: true,
            paranoid: true,
            timestamps: true,
            underscored: true,
            hooks: {
                beforeDestroy: changeEmailId,
                beforeCreate: hashPassword

            }
        });

        sequelize.sync({
            logging: console.log
        }).catch(function (error) {
            console.log(error);
        });
        return User;
    };

    var hashPassword = function (tbl3_users, options) {
        tbl3_users.password = bcrypt.hashSync(tbl3_users.password, saltRounds);
    }

    var changeEmailId = function (tbl3_users, options) {
        console.log("in destroy hook");
        tbl3_users.v_email = 'del-' + moment().unix() + tbl3_users.v_email;
    }

// clent Model

"use strict";
var DB = require('../db');
// const User = DB.User;

module.exports = function(sequelize, DataTypes) {
    var Client = sequelize.define("tbl4_clients", {
        i_user_id:DataTypes.INTEGER(11),
        i_company_established: DataTypes.INTEGER(11),
        v_registration_number : DataTypes.STRING(50),
        v_payment_options: DataTypes.STRING(50),
        v_web_mail_url: DataTypes.STRING(50),
        v_web_mail_email: DataTypes.STRING(50),
        v_web_mail_password: DataTypes.STRING(50), // encryption required?
        i_last_modified_by: DataTypes.INTEGER(11)
    },
    {
        classMethods: {
            associate: function (models) {
                //Client.belongsTo(DB.User, { foreignKey: 'i_user_id' });
                 Client.belongsTo(DB.User)
            }
        }
    }, 
    {
        freezeTableName: true,
        paranoid: true,
        timestamps: true,
        underscored: true,
    });

    sequelize.sync({
        // logging: console.log
    }).catch(function(error) {
        console.log(error);
    });
    return Client;
};

// include 
Client.findAll({
        include: [{
                model: User,
                attributes: ['id','v_last_name', 'v_first_name'],
                where: {'i_company_id': 1},
                required: false
            }],
        attributes: ['id','i_user_id']
    }).then(function (result) {
          res.json(result);
    });

Please help Thanks in advance



via sarang pathak

No comments:

Post a Comment