onDelete:“级联”不删除子行

时间:2018-08-09 10:45:18

标签: javascript node.js

这是作者表

const fieldSchema = {
author_id: {
    type: Sequelize.INTEGER,
    primaryKey:true,
    autoIncrement:true
},
name: {
    type: Sequelize.STRING,
    allowNull: false
}
const tableSchema = {
tableName: 'author,
order:'author_id'
};
const Term = db.define('author', fieldSchema, tableSchema);

这是图书模型。

const fieldSchema = {
book_id: {
    type: Sequelize.INTEGER,
    primaryKey:true,
    autoIncrement:true
},
name: {
    type: Sequelize.STRING,
    allowNull: false
}
author_id: {
    type: Sequelize.INTEGER,
    allowNull: false
}
const tableSchema = {
tableName: 'author,
order:'author_id'
};
const Term = db.define('author', fieldSchema, tableSchema);

Book.belongsTo(Author, {foreignKey: 'author_id', targetKey: 'author_id', as: 'author', onDelete: 'cascade'});

当我删除一位作者时,与该作者有关的书就不会删除。 当我们写onDelete:'cascade'应该可以,但是不可以。

我做错了什么。请帮帮我,否则您可以建议我另一种方式。

谢谢。

0 个答案:

没有答案