Home > Enterprise >  SQLite says table doesn't exist when I created it with Sequelize
SQLite says table doesn't exist when I created it with Sequelize

Time:11-29

I'm trying to understand how Sequelize works and I don't understand why I get SQLITE_ERROR: no such table: Users even though I created the table with sequelize.define. Code:

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

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

async function testdb() {
    try {
        await db.authenticate();
        console.log('db connected'); // test 1
    } catch (error) {
        console.error(error);
    }

    const User = db.define('User', {
        userName: {
            type: DataTypes.STRING,
            allowNull: false,
            unique: true,
        },
        email: {
            type: DataTypes.STRING,
            allowNull: false,
        },
        bio: {
            type: DataTypes.TEXT,
        },
    });

    console.log(db.models.User); // test 2

    const user = await User.create({
        userName: 'epic_user01',
        email: '[email protected]',
        bio: 'hello world!!!',
    });

    console.log(user.id); // test 3
}

testdb();

Tests 1 and 2 return successful before I get the error message.

CodePudding user response:

You have to call sync to actually create the tables, add this right after the define and before the create:

await User.sync();
  • Related