Home > Net >  Why do I get `no such table` even though I created the model?
Why do I get `no such table` even though I created the model?

Time:12-09

I don't understand why I get Error: SQLITE_ERROR: no such table: cities even though I created the model with Sequelize. I followed the same code that worked fine last time.
models/cities.js:

const { Sequelize, DataTypes } = require('sequelize');

const db = new Sequelize({
    dialect: 'sqlite',
    storage: './database.sqlite',
});

const Cities = db.define('cities', {
    userId: {
        type: DataTypes.STRING,
        unique: true,
        primaryKey: true,
        allowNull: false,
    },
    name: {
        type: DataTypes.STRING,
        allowNull: false,
    },
    // ...
    busStationsBuilt: {
        type: DataTypes.INTEGER,
        allowNull: false,
        defaultValue: 0,
    },
});

module.exports = { Cities };

db-init.js (run manually):

const { Sequelize } = require('sequelize');

const db = new Sequelize({
    dialect: 'sqlite',
    storage: './database.sqlite',
});

require('./models/cities.js');

const force = process.argv.includes('--force') || process.argv.includes('-f');

db.sync({ force }).then(async () => {
    console.log('Database synced.');

    db.close();
}).catch(console.error);

commands/found.js:

const { Cities } = require('../models/cities.js');
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');

module.exports = {
    data: new SlashCommandBuilder()
        .setName('found')
        .setDescription('Found your city!')
        .addStringOption(option =>
            option
                .setName('name')
                .setDescription('The name of your city!')
                .setRequired(true)
        ),
    async execute(interaction) {
        const name = interaction.options.getString('name');

        const cityAlreadyExists = await Cities.findOne({ where: { userId: interaction.user.id } });

        if (!cityAlreadyExists) {
            try {
                const city = await Cities.create({
                    userId: interaction.user.id,
                    name: name,
                });

                const cityEmbed = new EmbedBuilder()
                    .setColor(0x73a0d0)
                    .setTitle(city.name)
                    .setDescription('Here are your city\'s stats:')
                    .addFields(
                        { name: '           
  • Related