{"record":{"id":"0e14fc4e008e32e9","repo":"Dokploy/dokploy","slug":"internal-server-error-0e14fc","errorCode":"INTERNAL_SERVER_ERROR","errorMessage":"Failed to create organization","messagePattern":"Failed to create organization","errorType":"exception","errorClass":"TRPCError","httpStatus":500,"severity":"error","filePath":"apps/dokploy/server/api/routers/organization.ts","lineNumber":56,"sourceCode":"\t\t\t}\n\n\t\t\tif (IS_CLOUD) {\n\t\t\t\tawait assertOrganizationLimit(ctx.user.id);\n\t\t\t}\n\n\t\t\tconst result = await db\n\t\t\t\t.insert(organization)\n\t\t\t\t.values({\n\t\t\t\t\t...input,\n\t\t\t\t\tslug: nanoid(),\n\t\t\t\t\tcreatedAt: new Date(),\n\t\t\t\t\townerId: ctx.user.id,\n\t\t\t\t})\n\t\t\t\t.returning()\n\t\t\t\t.then((res) => res[0]);\n\n\t\t\tif (!result) {\n\t\t\t\tthrow new TRPCError({\n\t\t\t\t\tcode: \"INTERNAL_SERVER_ERROR\",\n\t\t\t\t\tmessage: \"Failed to create organization\",\n\t\t\t\t});\n\t\t\t}\n\n\t\t\t// Check if this is the user's first organization\n\t\t\tconst existingMemberships = await db.query.member.findMany({\n\t\t\t\twhere: eq(member.userId, ctx.user.id),\n\t\t\t});\n\n\t\t\tawait db.insert(member).values({\n\t\t\t\torganizationId: result.id,\n\t\t\t\trole: \"owner\",\n\t\t\t\tcreatedAt: new Date(),\n\t\t\t\tuserId: ctx.user.id,\n\t\t\t});\n\t\t\tawait audit(ctx, {\n\t\t\t\taction: \"create\",","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/Dokploy/dokploy/blob/546686ea3587f12ec5652217dedd9f7960fb6d15/apps/dokploy/server/api/routers/organization.ts#L38-L74","documentation":"Defensive check after inserting a new organization row with Drizzle (.returning().then(res => res[0])): if the insert returned no row, the mutation throws INTERNAL_SERVER_ERROR. This only happens if the INSERT silently returns empty — rare, but can occur with broken BEFORE INSERT triggers, schema drift, or a DB driver/serialization hiccup.","triggerScenarios":"The organization INSERT executes but `.returning()` yields zero rows — e.g. a trigger swallowing the insert, mismatched schema after failed migration, or unusual Postgres behavior (RULE rewriting the statement).","commonSituations":"Database restored from a dump with custom triggers/rules; partial upgrade left the returning-capable schema inconsistent; transient connection pool failure mid-statement.","solutions":["Retry the create — transient driver issues resolve themselves","Inspect the organizations table for triggers/rules: SELECT event_manipulation, action_statement FROM information_schema.triggers WHERE event_object_table='organization'","Verify schema is up to date (run migrations / dokploy upgrade)","Check Postgres logs for errors at the moment of the insert"],"exampleFix":"// before\nawait trpc.organization.create.mutate({ name }); // INTERNAL_SERVER_ERROR: Failed to create organization\n\n// after: drop the rogue trigger, then retry\n-- DROP TRIGGER skip_insert ON organization;\nawait trpc.organization.create.mutate({ name });","handlingStrategy":"retry","validationCode":"const healthy = await db.execute(sql`select 1`);\nif (!healthy) throw new Error('DB not ready');","typeGuard":"null","tryCatchPattern":"try { await create(input); } catch (e) { if (getTRPCCode(e) === 'INTERNAL_SERVER_ERROR') await backoffRetry(create, input, 2); else throw e; }","preventionTips":["Don't add custom triggers/rules to Dokploy's tables","Retry once — empty .returning() is usually transient","Keep schema migrations in sync"],"tags":["trpc","database","postgres","drizzle","trigger"],"backgroundTag":"database-insert-failed","analyzedSha":"546686ea3587f12ec5652217dedd9f7960fb6d15","analyzedAt":"2026-08-27T05:18:58.095Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}