mastra-ai/mastra · error

Factory project not found for this organization.

Error message

Factory project not found for this organization.

What it means

Thrown when creating a connection whose input.factoryProjectId does not resolve to a factory project row in the given org. Connections must reference an existing factory project within the same organization.

Source

Thrown at mastracode/factory/src/storage/domains/source-control/base.ts:746

          const project = await db().findOne<Record<string, unknown>>(FACTORY_PROJECTS, {
            id: factoryProjectId,
            org_id: orgId,
          });
          if (!project) return [];
          return (
            await db().findMany<ConnectionDbRow>(CONNECTIONS, {
              factory_project_id: factoryProjectId,
              integration_id: integrationId,
            })
          ).map(toConnection);
        },
        get: getConnection,
        create: async input => {
          const project = await db().findOne<Record<string, unknown>>(FACTORY_PROJECTS, {
            id: input.factoryProjectId,
            org_id: input.orgId,
          });
          if (!project) throw new Error('Factory project not found for this organization.');
          await requireInstallation({ orgId: input.orgId, id: input.installationId });
          try {
            const row = await db().insertOne<ConnectionDbRow>(CONNECTIONS, {
              factory_project_id: input.factoryProjectId,
              integration_id: integrationId,
              installation_id: input.installationId,
              created_by_user_id: input.createdByUserId,
              created_at: new Date(),
            });
            return toConnection(row);
          } catch (error) {
            if (!(error instanceof UniqueViolationError)) throw error;
            const row = await db().findOne<ConnectionDbRow>(CONNECTIONS, {
              factory_project_id: input.factoryProjectId,
              integration_id: integrationId,
              installation_id: input.installationId,
            });
            if (!row) throw error;

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Verify the factory project exists in the same org before creating the connection
  2. Create the factory project first if it does not exist
  3. Check that input.orgId matches the project's org_id

Example fix

// before
await forIntegration.connections.create({ orgId, factoryProjectId: projectId, installationId });
// after
const project = await factoryProjects.get({ orgId, id: projectId });
if (!project) throw new Error(`Create project ${projectId} first`);
await forIntegration.connections.create({ orgId, factoryProjectId: projectId, installationId });
Defensive patterns

Strategy: validation

Validate before calling

const project = await db().findOne(FACTORY_PROJECTS, { id: factoryProjectId, org_id: orgId });
if (!project) throw new Error(`Factory project ${factoryProjectId} does not exist in org ${orgId}`);

Type guard

function hasFactoryProject(
  p: { id: string; org_id: string } | null,
  orgId: string
): p is { id: string; org_id: string } {
  return p !== null && p.org_id === orgId;
}

Try / catch

try {
  await connections.create(input);
} catch (e) {
  if (e instanceof Error && e.message.includes('Factory project not found')) {
    await createFactoryProject({ orgId, id: input.factoryProjectId });
    await connections.create(input);
  } else throw e;
}

Prevention

When it happens

Trigger: Calling connections.create with a factoryProjectId from another org, a deleted project, or a mistyped/nonexistent id.

Common situations: Cross-environment IDs (staging vs production); project deleted before linking; copying seed data between orgs.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/3420500e61dc4091. Report an issue: GitHub.