{"record":{"id":"da20c055ee7c2188","repo":"hcengineering/platform","slug":"createdoc-cannot-be-called-for-domain-model-classe","errorCode":null,"errorMessage":"createDoc cannot be called for DOMAIN_MODEL classes with non-model space","messagePattern":"createDoc cannot be called for DOMAIN_MODEL classes with non-model space","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"foundations/core/packages/core/src/operations.ts","lineNumber":114,"sourceCode":"\n  tx (tx: Tx): Promise<TxResult> {\n    return this.client.tx(tx)\n  }\n\n  async createDoc<T extends Doc>(\n    _class: Ref<Class<T>>,\n    space: Ref<Space>,\n    attributes: Data<T>,\n    id?: Ref<T>,\n    modifiedOn?: Timestamp,\n    modifiedBy?: PersonId\n  ): Promise<Ref<T>> {\n    const hierarchy = this.client.getHierarchy()\n    if (hierarchy.isDerived(_class, core.class.AttachedDoc)) {\n      throw new Error('createDoc cannot be used for objects inherited from AttachedDoc')\n    }\n    if (hierarchy.findDomain(_class) === DOMAIN_MODEL && space !== core.space.Model) {\n      throw new Error('createDoc cannot be called for DOMAIN_MODEL classes with non-model space')\n    }\n    const tx = this.txFactory.createTxCreateDoc(_class, space, attributes, id, modifiedOn, modifiedBy)\n    await this.tx(tx)\n    return tx.objectId\n  }\n\n  async addCollection<T extends Doc, P extends AttachedDoc>(\n    _class: Ref<Class<P>>,\n    space: Ref<Space>,\n    attachedTo: Ref<T>,\n    attachedToClass: Ref<Class<T>>,\n    collection: Extract<keyof T, string> | string,\n    attributes: AttachedData<P>,\n    id?: Ref<P>,\n    modifiedOn?: Timestamp,\n    modifiedBy?: PersonId\n  ): Promise<Ref<P>> {\n    const tx = this.txFactory.createTxCollectionCUD<T, P>(","sourceCodeStart":96,"sourceCodeEnd":132,"githubUrl":"https://github.com/hcengineering/platform/blob/63e28dc96483967b2fc21c881b3f1023c1de7718/foundations/core/packages/core/src/operations.ts#L96-L132","documentation":"createDoc also enforces that classes living in the model domain (DOMAIN_MODEL, i.e. classifier/definition objects) may only be created in core.space.Model. Creating a model-domain class into a user workspace space would corrupt the model, so the operation throws this Error before generating the tx.","triggerScenarios":"Calling createDoc with a class whose hierarchy.findDomain(_class) === 'model' while passing a non-model space (any user space), e.g. trying to instantiate a Class, Interface, Mixin, or plugin-defined classifier into a project space.","commonSituations":"Generic creation UIs that let users pick any registered class; scripts creating 'type' objects in workspaces instead of registering them via model tx/upgrade; confusion between model classes and data classes.","solutions":["If the object is a definition/classifier, create it via a model tx (upgrade/derive) rather than createDoc, or use space core.space.Model","If it is real user data, move the class out of the model domain (do not derive from Classifier/Doc-with-model-domain definitions)","Route model-domain creation through plugin model packages installed during upgrades","Add a pre-check hierarchy.findDomain(_class) === DOMAIN_MODEL ? core.space.Model : space in generic helpers"],"exampleFix":"// before\nawait ops.createDoc(core.class.State, projectSpace, stateAttrs)\n// after\nawait ops.createDoc(core.class.State, core.space.Model, stateAttrs) // model-domain classes require model space","handlingStrategy":"validation","validationCode":"const hierarchy = client.getHierarchy()\nconst domain = hierarchy.findDomain(_class)\nif (domain === 'model' && space !== core.space.Model) {\n  throw new Error(`${String(_class)} is a model-domain class; must use core.space.Model`)\n}\nawait ops.createDoc(_class, space, attributes)","typeGuard":null,"tryCatchPattern":"try {\n  await ops.createDoc(_class, space, attrs)\n} catch (err) {\n  if ((err as Error).message.includes('DOMAIN_MODEL')) {\n    return ops.createDoc(_class, core.space.Model, attrs) // or reject - model classes should not be user-created\n  }\n  throw err\n}","preventionTips":["Create classifier/definition objects via model txes and upgrades, not runtime createDoc","Use core.space.Model for anything whose findDomain is 'model'","Restrict user-facing creation UIs to data-domain classes","Keep model definitions in plugin model packages rather than workspace data"],"tags":["operations","model-domain","space-mismatch","validation"],"backgroundTag":"model-class-wrong-space","analyzedSha":"63e28dc96483967b2fc21c881b3f1023c1de7718","analyzedAt":"2026-08-29T15:21:27.377Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}