pubkey/rxdb · error · RxError
DB6
DB6
Error message
RxDB Error-Code: DB6. Hint: Error messages are not included in RxDB core to reduce build size. To show the full error messages and to ensure that you do not make any mistakes when using RxDB, use the dev-mode plugin when you are in development mode: https://rxdb.info/dev-mode.html?console=error
What it means
DB6 is thrown when a collection already exists in the internal store but with a different schema hash than the schema you are now passing to addCollections(). RxDB stores a hash of each collection's schema; re-creating a collection with a changed schema is not allowed because existing documents would not match. You must either keep the schema identical or create a schema migration / a new collection.
Source
Thrown at src/rx-database.ts:464
'rx-database-add-collection'
),
ensureNoStartupErrors(this)
]);
await Promise.all(
putDocsResult.error.map(async (error) => {
if (error.status !== 409) {
throw newRxError('DB12', {
database: this.name,
writeError: error
});
}
const docInDb: RxDocumentData<InternalStoreCollectionDocType> = ensureNotFalsy(error.documentInDb);
const collectionName = docInDb.data.name;
const schema = (schemas as any)[collectionName];
// collection already exists but has different schema
if (docInDb.data.schemaHash !== await schema.hash) {
throw newRxError('DB6', {
database: this.name,
collection: collectionName,
previousSchemaHash: docInDb.data.schemaHash,
schemaHash: await schema.hash,
previousSchema: docInDb.data.schema,
schema: ensureNotFalsy((jsonSchemas as any)[collectionName])
});
}
})
);
} catch (err) {
/**
* Close any pre-created storage instances on error.
* Some instances might have failed to create (rejected promise),
* so we catch and ignore errors during cleanup.
*/
await Promise.all(
Object.values(collectionStorageInstancePromises).map(View on GitHub (pinned to af6fb65f94)
Solutions
- Revert the schema to match the stored one, or increment the schema version and add a migrationStrategies entry for the new version.
- During development only, delete the persisted database (e.g. clear IndexedDB or call db.remove()) so the new schema is created fresh.
- Use RxDB's migration plugin: addCollections with the new versioned schema plus migrationStrategies so old documents are migrated.
- Do not mutate an existing collection's schema in place; design schema changes as version +1 with a migration.
Example fix
// before
const heroSchema = { title: 'hero', version: 0, properties: { name: { type: 'string' }, age: { type: 'number' } } };
await db.addCollections({ heroes: { schema: heroSchema } });
// after
const heroSchema = { title: 'hero', version: 1, properties: { name: { type: 'string' }, age: { type: 'number' } }, required: ['name'] };
await db.addCollections({
heroes: {
schema: heroSchema,
migrationStrategies: {
0: (oldDoc) => oldDoc
}
}
}); Defensive patterns
Strategy: validation
Validate before calling
const stored = await getStoredSchemaHash(db, 'heroes');
const current = await createRxSchema(heroSchema, db.hashFunction).hash;
if (stored && stored !== current) {
heroSchema.version += 1; // and add migrationStrategies
} Type guard
function schemasEqual(a: RxJsonSchema<any>, b: RxJsonSchema<any>): boolean {
return JSON.stringify(normalizeSchema(a)) === JSON.stringify(normalizeSchema(b));
} Try / catch
try {
await db.addCollections(defs);
} catch (err) {
if (isRxError(err) && err.code === 'DB6') {
console.error('Schema changed for existing collection; bump version and add migrationStrategies', err.parameters?.collection);
} else {
throw err;
}
} Prevention
- Treat schemas as immutable once shipped: every change is version+1 plus a migration strategy.
- In development, clear persisted storage (IndexedDB) when iterating on schemas, or call db.remove().
- Keep schema definitions in a versioned module so old versions remain available for migrations.
- Test schema upgrades against a copy of production data before release.
When it happens
Trigger: Calling addCollections() for a collection name that already exists on disk while the RxJsonSchema has been modified (added/removed/changed fields, changed version without migration strategy), so the computed schema hash differs from the stored one.
Common situations: Editing a schema during development while the old collection data persists in IndexedDB/LocalStorage; renaming or retyping fields; bumping schema version without providing a migration strategy; sharing one database between app versions with divergent schemas.
AI-assisted analysis of pubkey/rxdb@af6fb65f94 (2026-08-31).
Data as JSON: /api/errors/1ff0b2246e9514ff.
Report an issue: GitHub.