{"record":{"id":"b13feb6060f734b7","repo":"vxcontrol/pentagi","slug":"failed-to-create-container-in-database-w","errorCode":null,"errorMessage":"failed to create container in database: %w","messagePattern":"failed to create container in database: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"backend/pkg/docker/client.go","lineNumber":255,"sourceCode":"\t\t\"name\":     containerName,\n\t\t\"type\":     containerType,\n\t\t\"flow_id\":  flowID,\n\t\t\"work_dir\": workDir,\n\t\t\"host_dir\": hostDir,\n\t})\n\tlogger.Info(\"running container\")\n\n\tdbContainer, err := dc.db.CreateContainer(ctx, database.CreateContainerParams{\n\t\tType:     containerType,\n\t\tName:     containerName,\n\t\tImage:    config.Image,\n\t\tStatus:   database.ContainerStatusStarting,\n\t\tFlowID:   flowID,\n\t\tLocalID:  database.StringToNullString(fmt.Sprintf(\"tmp-id-%d\", flowID)),\n\t\tLocalDir: database.StringToNullString(hostDir),\n\t})\n\tif err != nil {\n\t\treturn database.Container{}, fmt.Errorf(\"failed to create container in database: %w\", err)\n\t}\n\n\tupdateContainerInfo := func(status database.ContainerStatus, localID string) {\n\t\tdbContainer, err = dc.db.UpdateContainerStatusLocalID(ctx, database.UpdateContainerStatusLocalIDParams{\n\t\t\tStatus:  status,\n\t\t\tLocalID: database.StringToNullString(localID),\n\t\t\tID:      dbContainer.ID,\n\t\t})\n\t\tif err != nil {\n\t\t\tlogger.WithError(err).Error(\"failed to update container info in database\")\n\t\t}\n\t}\n\n\tfallbackDockerImage := func() error {\n\t\tlogger = logger.WithField(\"image\", dc.defImage)\n\t\tlogger.Warn(\"try to use default image\")\n\t\tconfig.Image = dc.defImage\n","sourceCodeStart":237,"sourceCodeEnd":273,"githubUrl":"https://github.com/vxcontrol/pentagi/blob/ea665308baaff015b226f308438a68d929d0f29b/backend/pkg/docker/client.go#L237-L273","documentation":"RunContainer first persists a container row (status Starting, placeholder LocalID tmp-id-<flowID>) via dc.db.CreateContainer before touching the daemon. If the insert fails, the error is wrapped as \"failed to create container in database: %w\" and no Docker container is created. This wraps a PostgreSQL/SQLC error — inspect the %w cause for the real reason.","triggerScenarios":"dc.db.CreateContainer fails: database unreachable, FK violation (flow_id does not exist in flows table), unique constraint on container name (name reused for the same flow), NOT NULL violation, or connection pool exhausted.","commonSituations":"Postgres restarted or max_connections exhausted during a heavy run; a container name collides with a row left by a crashed earlier attempt; caller passes a flowID that was never created; DB migrations not applied so the containers table is missing columns.","solutions":["Read the wrapped cause in the error chain (database is down vs constraint violation) — check Postgres connectivity first (docker compose ps, pg_isready).","If unique-constraint on name: delete or reset the stale container row for that name/flow before retrying.","Verify the flow exists (SELECT from flows WHERE id=<flowID>) before running a container for it.","Run pending migrations (goose) if the schema predates new columns; check application DB logs for the exact constraint error."],"exampleFix":"-- before: retry fails forever on stale row\nSELECT * FROM containers WHERE name='<name>' AND flow_id=42;\n-- after\ndelete from containers where name='<name>' and flow_id=42 and status='starting';","handlingStrategy":"try-catch","validationCode":"// Before running the container, verify the flow and name are free:\nif _, err := db.GetFlow(ctx, flowID); err != nil {\n    return fmt.Errorf(\"flow %d not found: %w\", flowID, err)\n}\n// optionally delete stale 'starting' rows with the same name","typeGuard":null,"tryCatchPattern":"dbContainer, err := dc.db.CreateContainer(ctx, params)\nif err != nil {\n    var pqe *pgconn.PgError\n    if errors.As(err, &pqe) {\n        switch pqe.Code {\n        case \"23505\": // unique_violation → stale row, safe to clean+retry\n            return retryAfterCleanup()\n        case \"23503\": // foreign_key_violation → flow missing\n            return fmt.Errorf(\"flow %d does not exist\", flowID)\n        default:\n            return fmt.Errorf(\"db insert failed (%s): %w\", pqe.Code, err)\n        }\n    }\n    return err // connectivity: rely on pool/retry middleware\n}","preventionTips":["Keep DB migrations current and monitor Postgres connectivity (readiness probes).","Clean up 'starting' container rows from crashed runs before reuse.","Ensure flows are committed before spawning their containers.","Size the connection pool for peak concurrent flows."],"tags":["database","postgresql","docker"],"backgroundTag":"database-insert-failed","analyzedSha":"ea665308baaff015b226f308438a68d929d0f29b","analyzedAt":"2026-09-01T14:16:31.421Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}