gpt4 book ai didi

node.js - Sequelize - 如何在模型定义期间定义 ON DELETE 操作

转载 作者:行者123 更新时间:2023-12-03 22:16:24 24 4
gpt4 key购买 nike

请问是否可以在定义Sequelize模型时设置ON DELETE Action ,例如:

sequelize.define('oAuthAccessTokens', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
createdAt: Sequelize.DATE,
updatedAt: Sequelize.DATE,
accessToken: { type: Sequelize.STRING(256), allowNull: false },
expires: { type: Sequelize.DATE, allowNull: false },
scope: Sequelize.STRING(255),
clientId: {
type: Sequelize.STRING(80),
allowNull: false,
references: {
model: 'oAuthClients',
key: "clientId",
**onDelete: "cascade"** // here is my try
}
},
userId: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'oAuthUsers',
key: "id",
**onDelete: "cascade"** // here is my try
}
}
});

我知道可以将适当的选项传递给 hasOne() 方法,但是这样的模型定义呢?我希望有一个官方文档中没有提到的方法。

已更新

问题已经解决了。 onDelete 属性应该在 references 之外。

sequelize.define('oAuthAccessTokens', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
createdAt: Sequelize.DATE,
updatedAt: Sequelize.DATE,
accessToken: { type: Sequelize.STRING(256), allowNull: false },
expires: { type: Sequelize.DATE, allowNull: false },
scope: Sequelize.STRING(255),
clientId: {
type: Sequelize.STRING(80),
allowNull: false,
onDelete: "cascade",
references: {
model: 'oAuthClients',
key: "clientId",
}
},
userId: {
type: Sequelize.INTEGER,
onDelete: "cascade",
allowNull: false,
references: {
model: 'oAuthUsers',
key: "id"
}
}
});

最佳答案

问题已经解决了。 onDelete 属性应该在 references 之外。

sequelize.define('oAuthAccessTokens', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
createdAt: Sequelize.DATE,
updatedAt: Sequelize.DATE,
accessToken: { type: Sequelize.STRING(256), allowNull: false },
expires: { type: Sequelize.DATE, allowNull: false },
scope: Sequelize.STRING(255),
clientId: {
type: Sequelize.STRING(80),
allowNull: false,
onDelete: "cascade",
references: {
model: 'oAuthClients',
key: "clientId",
}
},
userId: {
type: Sequelize.INTEGER,
onDelete: "cascade",
allowNull: false,
references: {
model: 'oAuthUsers',
key: "id"
}
}
});

关于node.js - Sequelize - 如何在模型定义期间定义 ON DELETE 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41783428/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com