TacoSnack
TacoSnack

Reputation: 179

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

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.

Upvotes: 1

Views: 700

Answers (1)

Paulo Fernando
Paulo Fernando

Reputation: 3660

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

await User.sync();

Upvotes: 1

Related Questions