Skip to content
Advertisement

Sequelize table is not created, even though I used Sequelize.define()

Repository (code): https://github.com/JavascriptLearner815/spelta-discord-bot/tree/development

I am creating a Discord bot with Discord.js. I wanted to add three DM commands for the bot: dm, reply, and followup.

In order to reply to the last DM, I needed to use SQLITE. I made and required a database.js file to do that, using the discord.js guide for SQLite. But when I run the command dm, the table isn’t even created!

My code editor does in fact show the files database.js and database.sqlite.

I have also tried using node database.js.

In Discord, I get the messages from my alt and my main account, but it replies to my interaction with “Error! Either you or the recipient have disabled DMs!” This is actually my message that gets sent with a catch.

What could be the problem here?

P.S. If you prefer viewing the code right here on StackOverflow, here it is:

commands/dm.js:

const { SlashCommandBuilder } = require("@discordjs/builders")
const { DirectMessages } = require("../database")

module.exports = {
    data: new SlashCommandBuilder()
        .setName("dm")
        .setDescription("Directly messages a user!")
        .addUserOption(option =>
            option
                .setRequired(true)
                .setName("user")
                .setDescription("The user to message"))
        .addStringOption(option =>
            option
                .setRequired(true)
                .setName("message")
                .setDescription("The message to send")),
    async execute(interaction) {
        const user = interaction.options.getUser("user")
        const message = interaction.options.getString("message")

        try {
            await user.send(`**${interaction.user.tag} sent a message:** ${message}`)
            await interaction.user.send(`**Sent ${user.tag} a message:** ${message}`)
            await DirectMessages.create({
                message,
                from: interaction.user.id,
                to: user.id,
            })
            interaction.reply({ content: "Successfully sent your message!", ephemeral: true })
        }
        catch (error) {
            console.error(error)
            interaction.reply({ content: "Error! Either the recipient or yourself has disabled DMs!", ephemeral: true })
        }
    },
}

database.js:

const Sequelize = require("sequelize")

const sequelize = new Sequelize("database", "user", "password", {
    host: "localhost",
    dialect: "sqlite",
    logging: false,
    // SQLite only
    storage: "database.sqlite",
})

const DirectMessages = sequelize.define("dms", {
    message: Sequelize.TEXT,
    from: Sequelize.STRING,
    to: Sequelize.STRING,
})

module.exports = {
    sequelize,
    DirectMessages,
}

Error: SQLITE_ERROR: no such table: dms

Advertisement

Answer

await sequelize.sync({ force: true });
console.log("All models were synchronized successfully.");

you should check Sequelize Synchronizing all model

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement