🔒 Role Duplication Error in seedRole Function
Overview
A critical duplication error occurs during application startup when the Node.js container attempts to seed roles into the database. The operation fails due to an E11000 duplicate key error on the code field (e.g., "SUPER_ADMIN").
This indicates that the seeding script does not account for existing data and attempts to reinsert a role that already exists. This is a misconfiguration/data seeding issue that can cause container startup failures and affect the application availability.
Affected Endpoint and Files
- Script/Function:
seedRole function
- File: Likely within
src/seeders/roleSeeder.js or similar
- Database Collection:
test.role
- Index Involved:
code_1 (unique constraint on code field)
Evidence
Error traceback from logs:
2025-09-01 12:18:07 error: Error in seedRole function
E11000 duplicate key error collection: test.role index: code_1 dup key: { code: "SUPER_ADMIN" }
MongoBulkWriteError: E11000 duplicate key error collection: test.role index: code_1 dup key: { code: "SUPER_ADMIN" }
at OrderedBulkOperation.handleWriteError (...)
...
2025-09-01 12:18:08 error: Error Store routes failed !2. quadrant connection issue
Likely problematic code:
// Problem: insertMany or insert used without checking for existence or using upsert
await Role.insertMany([
{ code: "SUPER_ADMIN", ... },
...
]);
Steps to Reproduce
1. Ensure the `test.role` collection already contains the document with { code: "SUPER_ADMIN" }.
2. Start the Node.js container (which triggers the `seedRole` function).
3. Observe container logs – startup fails with MongoBulkWriteError due to duplicate key.
Expected Behavior
Seeding should not fail if the role already exists.
The `seedRole` function should be idempotent:
* It should either use upsert, or
* Check for the existence of each role before inserting.
Actual Behavior
The container fails to start due to a duplicate key error.
Critical system functionality (e.g., route registration) is blocked.
Impact
High risk:
* Application fails to start in certain environments (e.g., staging, prod) if roles were already seeded manually or from a previous deployment.
* Causes confusion and requires manual DB intervention.
* Affects CI/CD and scaling reliability.
Proposed Remediation
✔ Replace insertMany or insert with upsert logic:
* Use `updateOne` with `{ upsert: true }`
* Or write a loop to check for existing role by `code` before inserting
✔ Ensure the seeding script is idempotent
✔ Add logging around which roles were skipped/inserted
✔ Optional: wrap seeding logic in try-catch with meaningful errors
Example fix (pseudo-code):
await Role.updateOne(
{ code: "SUPER_ADMIN" },
{ $setOnInsert: { name: "Super Administrator", permissions: [...] } },
{ upsert: true }
);
🔒 Role Duplication Error in
seedRoleFunctionOverview
A critical duplication error occurs during application startup when the Node.js container attempts to seed roles into the database. The operation fails due to an E11000 duplicate key error on the
codefield (e.g.,"SUPER_ADMIN").This indicates that the seeding script does not account for existing data and attempts to reinsert a role that already exists. This is a misconfiguration/data seeding issue that can cause container startup failures and affect the application availability.
Affected Endpoint and Files
seedRolefunctionsrc/seeders/roleSeeder.jsor similartest.rolecode_1(unique constraint oncodefield)Evidence
Error traceback from logs:
Likely problematic code:
Steps to Reproduce
Expected Behavior
Actual Behavior
Impact
Proposed Remediation