{"record":{"id":"27acccb0c0fc0f20","repo":"n8n-io/n8n","slug":"cannot-operation-given-value-must-be-instance","errorCode":null,"errorMessage":"Cannot ${operation}, given value must be instance of entity class, instead object literal is given. Or you must specify an entity target to method call.","messagePattern":"Cannot (.+?), given value must be instance of entity class, instead object literal is given\\. Or you must specify an entity target to method call\\.","errorType":"exception","errorClass":"CannotDetermineEntityError","httpStatus":null,"severity":"error","filePath":"packages/@n8n/typeorm/src/persistence/EntityPersistExecutor.ts","lineNumber":76,"sourceCode":"\t\t}\n\n\t\ttry {\n\t\t\t// collect all operate subjects\n\t\t\tconst entities: ObjectLiteral[] = Array.isArray(this.entity) ? this.entity : [this.entity];\n\t\t\tconst entitiesInChunks =\n\t\t\t\tthis.options && this.options.chunk && this.options.chunk > 0\n\t\t\t\t\t? OrmUtils.chunk(entities, this.options.chunk)\n\t\t\t\t\t: [entities];\n\n\t\t\t// console.time(\"building subject executors...\");\n\t\t\tconst executors = await Promise.all(\n\t\t\t\tentitiesInChunks.map(async (entities) => {\n\t\t\t\t\tconst subjects: Subject[] = [];\n\n\t\t\t\t\t// create subjects for all entities we received for the persistence\n\t\t\t\t\tentities.forEach((entity) => {\n\t\t\t\t\t\tconst entityTarget = this.target ? this.target : entity.constructor;\n\t\t\t\t\t\tif (entityTarget === Object) throw new CannotDetermineEntityError(this.mode);\n\n\t\t\t\t\t\tlet metadata = this.connection\n\t\t\t\t\t\t\t.getMetadata(entityTarget)\n\t\t\t\t\t\t\t.findInheritanceMetadata(entity);\n\n\t\t\t\t\t\tsubjects.push(\n\t\t\t\t\t\t\tnew Subject({\n\t\t\t\t\t\t\t\tmetadata,\n\t\t\t\t\t\t\t\tentity: entity,\n\t\t\t\t\t\t\t\tcanBeInserted: this.mode === 'save',\n\t\t\t\t\t\t\t\tcanBeUpdated: this.mode === 'save',\n\t\t\t\t\t\t\t\tmustBeRemoved: this.mode === 'remove',\n\t\t\t\t\t\t\t\tcanBeSoftRemoved: this.mode === 'soft-remove',\n\t\t\t\t\t\t\t\tcanBeRecovered: this.mode === 'recover',\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t);\n\t\t\t\t\t});\n","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/n8n-io/n8n/blob/5ac6606e81f67bb9534255570cd4e86fd8101eee/packages/@n8n/typeorm/src/persistence/EntityPersistExecutor.ts#L58-L94","documentation":"`EntityPersistExecutor` resolves the entity target as `this.target ?? entity.constructor`. When you call `manager.save(plainObj)` (no bound target) and `plainObj` is a `{}`-literal, `entity.constructor` is `Object` — meaning TypeORM cannot tell which entity metadata to use. It throws `CannotDetermineEntityError` rather than guessing. Typed repositories have a target so they never hit this.","triggerScenarios":"Calling `dataSource.manager.save({ name: 'x' })` or `manager.remove({ id: 1 })` with a plain object literal and no first-argument entity target. Passing deserialized JSON directly (e.g. `req.body`) into `manager.save`.","commonSituations":"Using the bare EntityManager instead of a repository. Forgetting to pass the entity class as the first argument: `manager.save(EntityClass, dto)`.","solutions":["Pass the entity class as the target argument: `await manager.save(User, plainObject)`.","Instantiate the entity first: `manager.save(Object.assign(new User(), dto))`.","Use the typed `repository.save(...)` from `dataSource.getRepository(User)`, which already carries the target."],"exampleFix":"// before — plain object, no target → error\nawait dataSource.manager.save({ name: 'Alice', email: 'a@x.com' });\n\n// after — explicit target\nawait dataSource.manager.save(User, { name: 'Alice', email: 'a@x.com' });\n// or\nconst user = Object.assign(new User(), { name: 'Alice', email: 'a@x.com' });\nawait dataSource.manager.save(user);","handlingStrategy":"type-guard","validationCode":"// Ensure plain objects are paired with an entity target before persisting\nfunction isEntityInstance<T>(o: T, ctor: new () => T): o is T {\n  return o instanceof ctor;\n}\nasync function safeSave<T>(manager: EntityManager, ctor: new () => T, input: Partial<T>) {\n  const entity = Object.assign(new ctor(), input);\n  return manager.save(entity); // safe: target is ctor\n}","typeGuard":"function isPlainLiteral(o: unknown): o is Record<string, unknown> {\n  return o !== null && typeof o === 'object' && (o as any).constructor === Object;\n}","tryCatchPattern":"try { await manager.save(payload); } catch (e) { if (e instanceof CannotDetermineEntityError) { /* re-dispatch as manager.save(EntityClass, payload) */ } throw e; }","preventionTips":["Use typed repositories (`getRepository(Entity)`) instead of the bare EntityManager.","Instantiate entities (`new Entity()`) or hydrate via `Object.assign(new Entity(), dto)` before saving.","When using `manager.save`, always pass the entity class as the first argument."],"tags":["typeorm","persistence","entity-manager","usage-error"],"backgroundTag":null,"analyzedSha":"5ac6606e81f67bb9534255570cd4e86fd8101eee","analyzedAt":"2026-08-12T05:26:35.080Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}