skwidbreth
skwidbreth

Reputation: 8404

Sequelize in Node/Express - 'no such table: main.User` error

I'm trying to build a simple Node/Express app with Sequelize, but when I try to create a new record in my relational database, I am getting the error Unhandled rejection SequelizeDatabaseError: SQLITE_ERROR: no such table: main.User. Basically, I create a user in the Users table and then try to create a related address in the Addresses table - the user is successfully created but it fails with this error when creating the address... where is it getting the main prefix from in the table name? (full error readout below)...

First off, here's a rundown of my program...

So when I try to post to /createUser, the User will successfully be created and the console will say that a new Address has been created (INSERT INTO 'Addresses'...), but the address is NOT created and the following error is logged:

Unhandled rejection SequelizeDatabaseError: SQLITE_ERROR: no such table: main.User at Query.formatError (/Users/darrenklein/Desktop/Darren/NYCDA/WDI/projects/world_table/wt_test_app_1/node_modules/sequelize/lib/dialects/sqlite/query.js:348:14) at afterExecute (/Users/darrenklein/Desktop/Darren/NYCDA/WDI/projects/world_table/wt_test_app_1/node_modules/sequelize/lib/dialects/sqlite/query.js:112:29) at Statement.errBack (/Users/darrenklein/Desktop/Darren/NYCDA/WDI/projects/world_table/wt_test_app_1/node_modules/sqlite3/lib/sqlite3.js:16:21)

I've done this sort of thing with Sequelize once before a few months ago and it was successful, I cannot for the life of me figure out what I'm missing here. Why is the app looking for main.User, and how can I get it to look for the correct table? Thank you!

Upvotes: 10

Views: 12957

Answers (3)

Sina
Sina

Reputation: 775

For me, it worked when I assigned "the table name" to that model, instead of what was defined in my actual model/class.

So I had this:

      inAppNotificationId: {
        allowNull: false,
        type: DataTypes.UUID,
        references: {
          model: 'InAppNotifications',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
      }

And I changed it to the table's name, and it worked.

      inAppNotificationId: {
        allowNull: false,
        type: DataTypes.UUID,
        references: {
          model: 'in_app_notifications',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
      }

I assumed this could have been the issue in the question as well, since their table was also called Users, and changing to that, fixed it. Might have been nothing to do with "plural/singular".

Upvotes: 0

alacret
alacret

Reputation: 619

The same thing happens to me.

In my development and production database, I use MySQL; it seems to be that for MySQL you can use references.model and references.id the name of the model and the field in the model.

Locally I started to use SQLite for testing, and that doesn't work. It requires for you to indicate the table name and field exactly as it is in the referenced table.

Upvotes: 1

skwidbreth
skwidbreth

Reputation: 8404

Aha! A small error has derailed my entire operation. In the migration files, references.model must be pluralized!

references: {
    model: "Users",
    key: "id"
}

Boom.

Upvotes: 13

Related Questions