Sequelize插入数据重复问题排查与解决求助
Hey there! Let's figure out why your user is getting inserted twice every time you hit that API. I've looked through your code and have some common culprits to check, plus fixes to try.
First, Check for Duplicate Route Registrations
This is the most likely culprit here. Looking at your app setup:
const users = require('./routes/users'); app.register(users, { prefix: '/user' });
Double-check that this app.register line only runs once in your entire codebase. It’s easy to accidentally copy-paste it, or have it included in multiple files (like a dev config and production config). Also, make sure your routes/users file isn’t adding the same POST / route twice—no duplicate router.post('/', controller.create) lines!
Fix Your Model Import (Avoid Requiring It Per Request)
Right now, you’re requiring the User model inside the create function, which runs every time a request comes in. While Sequelize caches models, this is unnecessary and could lead to unexpected behavior. Move the import to the top of your controller file:
// At the top of your controller, not inside the function! const User = require('./models').User; exports.create = (req, res) => { User.create({ id: 0, name: 'Jon', age: 21, email: 'test@gameil.com', city: 1 }) .then(result => res.send(result)) .catch(error => res.status(500).send(error)); }
Verify the Request Isn’t Being Sent Twice
Grab Postman or curl and send a single request to your /user endpoint. If only one user is inserted, the problem is on the frontend—maybe a double-click on a button, or a retry mechanism in your frontend code (like Axios automatically retrying failed requests). If it still inserts twice, the issue is definitely in your backend.
Add Debug Logs to Trace the Problem
Add console logs to your create controller to see if the function is being called twice per request:
exports.create = (req, res) => { console.log("🚀 Received create user request at:", new Date().toISOString()); User.create({ id: 0, name: 'Jon', age: 21, email: 'test@gameil.com', city: 1 }) .then(result => { console.log("✅ Created user with ID:", result.id); res.send(result); }) .catch(error => { console.error("❌ Error creating user:", error); res.status(500).send(error); }); }
If you see two "Received create user request" logs for one API call, that confirms the route/controller is being triggered twice—go back to checking duplicate route registrations or middleware that might be reprocessing the request.
Check for Sequelize Hook or Initialization Issues
While less likely, make sure your User model doesn’t have any unintended hooks (like beforeCreate that’s calling create again) and that your Sequelize instance isn’t being initialized multiple times. Your model index file looks okay, but you can add a log there to confirm it only runs once when your app starts.
Final Checklist
- Ensure
app.register(users, { prefix: '/user' })runs exactly once. - No duplicate route definitions in your
routes/usersfile. - Model imports are at the top of controller files, not inside request handlers.
- Test with Postman/curl to rule out frontend duplicate requests.
- Use debug logs to trace if the controller function is called twice.
内容的提问来源于stack exchange,提问作者Guilherme Monteiro de Oliveira

