gpt4 book ai didi

javascript - 无法使用 Sequelize 更新(或保存)现有行

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

尝试 .update().save() 一行时出现此错误:

Unhandled rejection Error: You attempted to save an instance with no primary key,
this is not allowed since it would result in a global update

我尝试了文档用作示例的所有 4 种方式(定义和不定义我想要保存的属性),但没有任何效果。这是我更新的实际代码:

Sydney.databases.guilds.findOrCreate({
attributes: ['guildLocale'],
where: {
guildID: _guild.id,
},
defaults: {
guildID: _guild.id,
guildLocale: 'en_US',
guildPrefix: '?',
},
}).spread((guild, created) => {
guild.update({guildLocale: args[1]})
.then(() => console.log(7))
.catch((e) => throw e);
});

这是公会模型:

let model = sequelize.define('guild', {
guildID: {
field: 'guild_id',
type: DataTypes.STRING,
primaryKey: true,
},
guildLocale: {
field: 'guild_locale',
type: DataTypes.STRING,
},
guildPrefix: {
field: 'guild_prefix',
type: DataTypes.STRING,
},
}, {tableName: 'guilds'});

我在这里错过了什么?

最佳答案

我遇到了同样的问题。当您指定要从数据库中获取的属性而不包括属性中的主键时,就会发生这种情况。当您尝试保存时,它会抛出以下错误:

未处理的拒绝错误:您试图保存没有主键的实例,这是不允许的,因为它会导致全局更新

所以简单的解决方案是像这样在属性中包含主键:

Sydney.databases.guilds.findOrCreate({
attributes: ['guildLocale', 'guildID'], // include guideID here!!
where: {
guildID: _guild.id,
},
defaults: {
guildID: _guild.id,
guildLocale: 'en_US',
guildPrefix: '?',
},
}).spread((guild, created) => {
guild.update({guildLocale: args[1]})
.then(() => console.log(7))
.catch((e) => throw e);
});

关于javascript - 无法使用 Sequelize 更新(或保存)现有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52702661/

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