【问题标题】:sequelize find data that is associatedsequelize 查找关联的数据
【发布时间】:2018-06-29 21:54:08
【问题描述】:

我有 2 个模型 ProphetTask,它们有 m:n 关系:

先知

const prophet = sequelize.define('prophets', {
    name: {
        type: Sequelize.STRING,
        primaryKey: true,
        unique: true
    }
});
prophet.relationship = function(task) {
    prophet.belongsToMany(task, {
        through: 'prophetTasks',
        as: 'tasks',
        foreignKey: 'prophetName'
    });
};

任务

const task = sequelize.define('tasks', {
    name: {
        type: Sequelize.STRING,
        primaryKey: true,
        unique: true
    }
});
task.relationship = function(prophet) {
    task.belongsToMany(prophet, {
        through: 'prophetTasks',
        as: 'prophets',
        foreignKey: 'taskName'
    });
};

编辑: 我的问题是有时我必须更新可能会删除与任务的某些关系的先知,但我不知道如何删除与任何先知没有关系的任务。

我相信我应该找到所有不属于 prophetTasks 表的任务,但我不知道如何使用 sequelize 查询它

【问题讨论】:

    标签: javascript sequelize.js


    【解决方案1】:

    您可以使用ON DELETE CASCADE

    因此,每当删除该行时,将删除包含它作为外键的数据。

    【讨论】:

    • 对不起,我搞砸了我的问题。我的意思是更新先知,而不是删除他们:\
    猜你喜欢
    • 2013-09-21
    • 2018-12-02
    • 2021-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-02
    相关资源
    最近更新 更多