My issue would be that when the below code runs, it builds the MySQL query and runs it, however, it throws this error: ValidationError [SequelizeValidationError]: notNull Violation: Guilds.GuildPremium cannot be null
.
However, these fields have default values defined in the super init method in the Model. What is the purpose of defining default values in the Model, if the instance of said Model is not inheriting the properties of the Model?
Also, do I have to define default values again in the code snippet below?
Thank you for your patience and time :)
The code snippet that ran:
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!`)
);
};
});
Model Guilds:
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
});
};
Legend:
- Guilds = class Guilds extends Model
- Guild = instance of class Discord.Guild
Log of the event:
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]: ]
CodePudding user response:
Default values don't work in your code because you indicated incorrect option for that. Replace default
with defaultValue
and it does the trick:
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?"
}
See Default values in the official documentation.