Discord Bot报错SequelizeDatabaseError:SQLITE_ERROR无users表求助
Hey there, let's break down why this error is happening and how to fix it quickly:
The Root Cause
This error means your SQLite database file (database.sqlite) doesn't have a users table at all. Sequelize can't query a table that never got created, so we need to make sure our database initialization step actually runs correctly.
Step 1: Run the Database Initialization Script
You've got a dbInit.js file that's supposed to create all your tables and seed the shop data—but if you never ran it, those tables don't exist! Open your terminal, navigate to your project folder, and run:
node dbInit.js
If you need to wipe existing data and start fresh (useful for testing), add the --force flag:
node dbInit.js --force
This tells Sequelize to sync all your models (Users, CurrencyShop, UserItems) to the SQLite database, creating the missing users table along with the others.
Step 2: Double-Check Model & Sync Consistency
Take a quick look at your Users.js model: you explicitly named the table users (lowercase), which matches what Sequelize is looking for. Also confirm that dbInit.js is actually importing the Users model—your code already has this line, which is good:
sequelize.import('models/Users');
Just make sure the file path is correct (your models folder should be in the same directory as dbInit.js).
Step 3: Verify Database Connection in main.js
Your dbObjects.js creates a Sequelize instance, and main.js uses it to fetch user balances. Make sure the configuration in dbObjects.js is identical to dbInit.js—especially the storage path:
// Both files should point to the same database.sqlite file const sequelize = new Sequelize('database', 'username', 'password', { host: 'localhost', dialect: 'sqlite', logging: false, storage: 'database.sqlite', });
If the paths don't match, main.js is connecting to a different (empty) database file, hence the missing table.
Step 4: Optional: Auto-Sync in main.js (For Development Only)
If you want to avoid running dbInit.js every time you change models, you can add a sync call in your bot's ready event (don't do this in production—it can accidentally wipe data):
First, import the sequelize instance from dbObjects.js in main.js:
const { Users, CurrencyShop, sequelize } = require('F:/Users/User/Desktop/SuccessBot/dbObjects.js');
Then update the ready event:
client.once('ready', async () => { // Sync models to the database on bot start await sequelize.sync(); const storedBalances = await Users.findAll(); storedBalances.forEach(b => currency.set(b.user_id, b)); console.log('Radar Success is online!'); });
Step 5: Confirm the Database File Exists
After running dbInit.js, check your project folder for the database.sqlite file. You can use a tool like DB Browser for SQLite to open it and verify that the users table is present. If it's still missing, check your terminal for errors when running dbInit.js—there might be a typo or path issue you missed.
Quick Note on Your Token Leak
Since you mentioned your bot token was leaked and your server was compromised, make sure you generate a new token in the Discord Developer Portal and replace the empty token variable in main.js immediately. Never share your bot token with anyone!
内容的提问来源于stack exchange,提问作者Zach C

