sequlize js中的自引用外键

时间:2018-12-17 10:41:06

标签: javascript mysql sequelize.js

我有一个模特 员工 PLAY [Get start timestamp] ****************************************************************************************************************************************************************** TASK [Gathering Facts] ********************************************************************************************************************************************************************** ok: [cloud] TASK [set_fact] ***************************************************************************************************************************************************************************** ok: [cloud] PLAY [Prepare to run the workload] ********************************************************************************************************************************************************** TASK [Gathering Facts] ********************************************************************************************************************************************************************** ok: [cloud] TASK [include_tasks] ************************************************************************************************************************************************************************ included: /home/cello/cello/src/operator-dashboard/agent/ansible/roles/cloud_azure/prepare/tasks/apply.yml for cloud TASK [Setup env specific variables] ********************************************************************************************************************************************************* ok: [cloud] TASK [Retrieve azure specific image details] ************************************************************************************************************************************************ ok: [cloud] TASK [Print azure image results] ************************************************************************************************************************************************************ ok: [cloud] => { "found_images": { "ansible_facts": { "azure_vmimages": [] }, "changed": false, "failed": false } } TASK [Create azure resource group] ********************************************************************************************************************************************************** fatal: [cloud]: FAILED! => {"changed": false, "msg": "Error creating or updating resource group myresourceGroup - Azure Error: SubscriptionNotFound\nMessage: The subscription '********' could not be found."} to retry, use: --limit @/home/cello/cello/src/operator-dashboard/agent/ansible/provcluster.retry PLAY RECAP ********************************************************************************************************************************************************************************** cloud : ok=7 changed=0 unreachable=0 failed=1

此处(id,first_name,last_name,manager_id)是一个自引用外键,它引用同一表的manager_id列。 如何在续集中实现这种用例。

我试过了,这不起作用

id

1 个答案:

答案 0 :(得分:1)

您可以定义关联:

employee.belongsTo(employee, {as: "Manager"});
employee.hasMany(employee, { as: "Employee", foreignKey: "manager_id", useJunctionTable: false });

然后像

一样使用它
employee.findAll({
    include : {
        model : employee ,
        as : 'Manager'
    }
})

OR

您可以使用 sequelize-hierarchy

var employee = sequelize.define('employee', {
    name: Sequelize.STRING,
    manager_id: {
        type: Sequelize.INTEGER,
        hierarchy: true
    }
});

employee.findAll({ hierarchy: true }).then(function(results) {
    // results = [
    //  { id: 1, manager_id: null, name: 'a', children: [
    //      { id: 2, manager_id: 1, name: 'ab', children: [
    //          { id: 3, manager_id: 2, name: 'abc' }
    //      ] }
    //  ] }
    // ]
});