Sequelize MySQL 模型扩展为 class findOrCreate 方法无法创建记录

Sequelize MySQL Model extended as class findOrCreate method fails to create record

我的问题是,当下面的代码运行时,它会构建 MySQL 查询并运行它,但是,它会抛出此错误:ValidationError [SequelizeValidationError]: notNull Violation: Guilds.GuildPremium cannot be null

但是,这些字段具有在模型中的超级初始化方法中定义的默认值。如果所述模型的实例不继承模型的属性,那么在模型中定义默认值的目的是什么?
另外,我是否必须在下面的代码片段中再次定义默认值?

感谢您的耐心和时间:)

运行:

的代码片段
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
    });
};

图例:

事件日志:

 
    2021-11-21T15:01:05.998950+00:00 app[worker.1]: Executing (): SELECT `GuildID`, `DiscordGuildID`, `GuildPremium`, `DiscordGuildPrefix`, `GuildActive`, `createdAt`, `updatedAt` FROM `Guilds` AS `Guilds` WHERE `Guilds`.`DiscordGuildID` = '728450482284134461' LIMIT 1;   
errors: [
    2021-11-21T15:01:06.039336+00:00 app[worker.1]:     ValidationErrorItem {
    2021-11-21T15:01:06.039336+00:00 app[worker.1]:       message: 'Guilds.GuildPremium cannot be null',
    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]:       path: 'GuildPremium',
    2021-11-21T15:01:06.039337+00:00 app[worker.1]:       value: null,
    2021-11-21T15:01:06.039337+00:00 app[worker.1]:       origin: '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]:           updatedAt: 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 app[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]:           attributes: undefined,
    2021-11-21T15:01:06.039341+00:00 app[worker.1]:           include: undefined,
    2021-11-21T15:01:06.039341+00:00 app[worker.1]:           raw: undefined,
    2021-11-21T15:01:06.039341+00:00 app[worker.1]:           silent: undefined
    2021-11-21T15:01:06.039341+00:00 app[worker.1]:         },
    2021-11-21T15:01:06.039341+00:00 app[worker.1]:         isNewRecord: true
    2021-11-21T15:01:06.039342+00:00 app[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]:       validatorName: null,
    2021-11-21T15:01:06.039342+00:00 app[worker.1]:       validatorArgs: []
    2021-11-21T15:01:06.039342+00:00 app[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]:       message: 'Guilds.DiscordGuildPrefix cannot be null',
    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]:       path: 'DiscordGuildPrefix',
    2021-11-21T15:01:06.039343+00:00 app[worker.1]:       value: null,
    2021-11-21T15:01:06.039343+00:00 app[worker.1]:       origin: '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]:           updatedAt: 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 app[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 app[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]:           attributes: undefined,
    2021-11-21T15:01:06.039345+00:00 app[worker.1]:           include: undefined,
    2021-11-21T15:01:06.039345+00:00 app[worker.1]:           raw: undefined,
    2021-11-21T15:01:06.039345+00:00 app[worker.1]:           silent: undefined
    2021-11-21T15:01:06.039346+00:00 app[worker.1]:         },
    2021-11-21T15:01:06.039347+00:00 app[worker.1]:         isNewRecord: true
    2021-11-21T15:01:06.039347+00:00 app[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]:       validatorName: null,
    2021-11-21T15:01:06.039348+00:00 app[worker.1]:       validatorArgs: []
    2021-11-21T15:01:06.039348+00:00 app[worker.1]:     }
    2021-11-21T15:01:06.039348+00:00 app[worker.1]:   ] 

默认值在您的代码中不起作用,因为您为此指定了错误的选项。用 defaultValue 替换 default 就可以了:

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