Sequelize MySQL 模型扩展为类 findOrCreate 方法无法创建记录
Posted
技术标签:
【中文标题】Sequelize MySQL 模型扩展为类 findOrCreate 方法无法创建记录【英文标题】:Sequelize MySQL Model extended as class findOrCreate method fails to create record 【发布时间】:2021-12-31 12:20:41 【问题描述】:我的问题是,当下面的代码运行时,它会构建 mysql 查询并运行它,但是,它会抛出这个错误:ValidationError [SequelizeValidationError]: notNull Violation: Guilds.GuildPremium cannot be null
。
但是,这些字段在模型中的 super init 方法中定义了默认值。如果模型的实例没有继承模型的属性,那么在模型中定义默认值的目的是什么? 另外,我是否必须在下面的代码 sn-p 中再次定义默认值?
感谢您的耐心和时间:)
运行的代码sn-p:
const NewGuild, created = Guilds.findOrCreate(
where:
DiscordGuildID: Guild.id
)
.then(() =>
if(created)
console.log(`Guild $Guild.id added to Database!`);
else
NewGuild
.update(
GuildActive: true
)
.then(() =>
console.log(`Guild $Guild.id reactivated in Database!`)
);
;
);
模型公会:
module.exports = class Guilds extends Model
static init(sequelize)
return super.init(
GuildID:
type: DataTypes.INTEGER.UNSIGNED,
autoIncrement: true,
primaryKey: true,
comment: "Guild Database ID"
,
DiscordGuildID:
type: DataTypes.INTEGER.UNSIGNED,
allowNull: false
,
GuildPremium:
type: DataTypes.BOOLEAN,
allowNull: false,
default: false
,
DiscordGuildPrefix:
type: DataTypes.TEXT,
allowNull: false,
default: process.env.DefaultPrefix
,
GuildActive:
type: DataTypes.BOOLEAN,
default: true,
comment: "Bot active in Guild?"
,
tableName: "Guilds",
modelName: "Guilds",
timestamps: true,
sequelize
);
;
传说:
Guilds = 类 Guilds 扩展模型 Guild = Discord.Guild 类的实例事件记录:
2021-11-21T15:01:05.998950+00:00 app[worker.1]: 执行 (): SELECT `GuildID`, `DiscordGuildID`, `GuildPremium`, `DiscordGuildPrefix`, `GuildActive`, `createdAt`, ` updatedAt` FROM `Guilds` AS `Guilds` WHERE `Guilds`.`DiscordGuildID` = '728450482284134461' LIMIT 1; 错误:[ 2021-11-21T15:01:06.039336+00:00 app[worker.1]: ValidationErrorItem 2021-11-21T15:01:06.039336+00:00 app[worker.1]:消息:'Guilds.GuildPremium 不能为空', 2021-11-21T15:01:06.039337+00:00 app[worker.1]: type: 'notNull Violation', 2021-11-21T15:01:06.039337+00:00 app[worker.1]: 路径: 'GuildPremium', 2021-11-21T15:01:06.039337+00:00 app[worker.1]: 值: null, 2021-11-21T15:01:06.039337+00:00 app[worker.1]: 来源: 'CORE', 2021-11-21T15:01:06.039337+00:00 app[worker.1]:instance: Guilds 2021-11-21T15:01:06.039337+00:00 app[worker.1]: dataValues: 2021-11-21T15:01:06.039338+00:00 app[worker.1]: GuildID: null, 2021-11-21T15:01:06.039338+00:00 app[worker.1]: DiscordGuildID: '728450482284134461', 2021-11-21T15:01:06.039338+00:00 app[worker.1]: 更新时间: 2021-11-21T15:01:06.017Z, 2021-11-21T15:01:06.039338+00:00 app[worker.1]: createdAt: 2021-11-21T15:01:06.017Z 2021-11-21T15:01:06.039339+00:00 应用 [worker.1]: , 2021-11-21T15:01:06.039339+00:00 app[worker.1]:_previousDataValues: DiscordGuildID: undefined , 2021-11-21T15:01:06.039339+00:00 app[worker.1]: _changed: Set(1) 'DiscordGuildID' , 2021-11-21T15:01:06.039340+00:00 app[worker.1]:_options: 2021-11-21T15:01:06.039340+00:00 app[worker.1]: isNewRecord: true, 2021-11-21T15:01:06.039340+00:00 app[worker.1]:_schema:null, 2021-11-21T15:01:06.039340+00:00 app[worker.1]:_schemaDelimiter:'', 2021-11-21T15:01:06.039341+00:00 app[worker.1]:属性:未定义, 2021-11-21T15:01:06.039341+00:00 app[worker.1]:包括:未定义, 2021-11-21T15:01:06.039341+00:00 app[worker.1]: raw: undefined, 2021-11-21T15:01:06.039341+00:00 应用程序 [worker.1]:静默:未定义 2021-11-21T15:01:06.039341+00:00 应用程序 [worker.1]: , 2021-11-21T15:01:06.039341+00:00 应用 [worker.1]: isNewRecord: true 2021-11-21T15:01:06.039342+00:00 应用 [worker.1]: , 2021-11-21T15:01:06.039342+00:00 app[worker.1]: validatorKey: 'is_null', 2021-11-21T15:01:06.039342+00:00 app[worker.1]: 验证器名称: null, 2021-11-21T15:01:06.039342+00:00 应用程序[worker.1]:验证器参数:[] 2021-11-21T15:01:06.039342+00:00 应用 [worker.1]: , 2021-11-21T15:01:06.039342+00:00 app[worker.1]: ValidationErrorItem 2021-11-21T15:01:06.039342+00:00 app[worker.1]:消息:'Guilds.DiscordGuildPrefix 不能为空', 2021-11-21T15:01:06.039343+00:00 app[worker.1]: type: 'notNull Violation', 2021-11-21T15:01:06.039343+00:00 app[worker.1]: 路径: 'DiscordGuildPrefix', 2021-11-21T15:01:06.039343+00:00 app[worker.1]: 值: null, 2021-11-21T15:01:06.039343+00:00 app[worker.1]: 来源: 'CORE', 2021-11-21T15:01:06.039343+00:00 app[worker.1]:instance: Guilds 2021-11-21T15:01:06.039343+00:00 app[worker.1]: dataValues: 2021-11-21T15:01:06.039344+00:00 app[worker.1]: GuildID: null, 2021-11-21T15:01:06.039344+00:00 app[worker.1]: DiscordGuildID: '728450482284134461', 2021-11-21T15:01:06.039344+00:00 app[worker.1]: 更新时间: 2021-11-21T15:01:06.017Z, 2021-11-21T15:01:06.039344+00:00 app[worker.1]: createdAt: 2021-11-21T15:01:06.017Z 2021-11-21T15:01:06.039344+00:00 应用 [worker.1]: , 2021-11-21T15:01:06.039344+00:00 app[worker.1]:_previousDataValues:DiscordGuildID:undefined, 2021-11-21T15:01:06.039344+00:00 app[worker.1]: _changed: Set(1) 'DiscordGuildID' , 2021-11-21T15:01:06.039345+00:00 app[worker.1]:_options: 2021-11-21T15:01:06.039345+00:00 应用 [worker.1]: isNewRecord: true, 2021-11-21T15:01:06.039345+00:00 app[worker.1]:_schema:null, 2021-11-21T15:01:06.039345+00:00 app[worker.1]:_schemaDelimiter:'', 2021-11-21T15:01:06.039345+00:00 app[worker.1]:属性:未定义, 2021-11-21T15:01:06.039345+00:00 app[worker.1]:包括:未定义, 2021-11-21T15:01:06.039345+00:00 app[worker.1]: raw: undefined, 2021-11-21T15:01:06.039345+00:00 应用程序 [worker.1]:静默:未定义 2021-11-21T15:01:06.039346+00:00 应用 [worker.1]: , 2021-11-21T15:01:06.039347+00:00 应用程序 [worker.1]: isNewRecord: true 2021-11-21T15:01:06.039347+00:00 应用程序 [worker.1]: , 2021-11-21T15:01:06.039347+00:00 app[worker.1]: validatorKey: 'is_null', 2021-11-21T15:01:06.039347+00:00 app[worker.1]: 验证器名称: null, 2021-11-21T15:01:06.039348+00:00 应用程序[worker.1]:验证器参数:[] 2021-11-21T15:01:06.039348+00:00 应用 [worker.1]: 2021-11-21T15:01:06.039348+00:00 app[worker.1]: ]【问题讨论】:
【参考方案1】:默认值在您的代码中不起作用,因为您为此指明了不正确的选项。将 default
替换为 defaultValue
就可以了:
GuildPremium:
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false
,
DiscordGuildPrefix:
type: DataTypes.TEXT,
allowNull: false,
defaultValue: process.env.DefaultPrefix
,
GuildActive:
type: DataTypes.BOOLEAN,
defaultValue: true,
comment: "Bot active in Guild?"
参见官方文档中的Default values。
【讨论】:
哦,是的,我总是把 2 混为一谈。谢谢 :)以上是关于Sequelize MySQL 模型扩展为类 findOrCreate 方法无法创建记录的主要内容,如果未能解决你的问题,请参考以下文章