Thursday 20 April 2017

Two foreign Key of same table in one table in sequelize

my team member model :-

var teamMember = {
    id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    level: DataTypes.INTEGER,
    supervisorId: {
        type: DataTypes.INTEGER,
        references: {
            model: "employees",
            key: "id"
        }
    },
    employeeId: {
        type: DataTypes.INTEGER,
        unique: true,
        references: {
            model: "employees",
            key: "id"
        }
    }

and there is employee model

mapping:-

db.employee.hasOne(db.teamMember);
db.teamMember.belongsTo(db.employee);

my query function

        db.teamMember.findOne({
        where: { employeeId: req.employee.id },
        include: [db.employee]

    })
    .then(teamMember => {
        if (!teamMember) {
            throw ('no teamMember found');
        }
       consol.log(teamMember)
    })

my teamMember table is like=

id------employeeId------supervisorId

2 ----------- 4 ------------- 5

Problem is -: so when i m asking for row in teamMember whose employeeId is 4. that should be include with supervisorId(JOIN) and it returns row with employee included of 4 (id) . i want employee of 5th id .

Both supervisorId and employeeId are reffer to employee table.



via hardy

No comments:

Post a Comment