{"record":{"id":"24275ea3e240f299","repo":"crowdsecurity/crowdsec","slug":"creating-machine-s-w","errorCode":null,"errorMessage":"creating machine '%s': %w","messagePattern":"creating machine '(.+?)': %w","errorType":"exception","errorClass":"InsertFail","httpStatus":null,"severity":"error","filePath":"pkg/database/machines.go","lineNumber":111,"sourceCode":"\t\t\t}\n\n\t\t\treturn machine, nil\n\t\t}\n\n\t\treturn nil, fmt.Errorf(\"user '%s': %w\", *machineID, UserExists)\n\t}\n\n\tmachine, err := c.Ent.Machine.\n\t\tCreate().\n\t\tSetMachineId(*machineID).\n\t\tSetPassword(string(hashPassword)).\n\t\tSetIpAddress(ipAddress).\n\t\tSetIsValidated(isValidated).\n\t\tSetAuthType(authType).\n\t\tSave(ctx)\n\tif err != nil {\n\t\tc.Log.Warningf(\"CreateMachine : %s\", err)\n\t\treturn nil, fmt.Errorf(\"creating machine '%s': %w\", *machineID, InsertFail)\n\t}\n\n\treturn machine, nil\n}\n\nfunc (c *Client) QueryMachineByID(ctx context.Context, machineID string) (*ent.Machine, error) {\n\tmachine, err := c.Ent.Machine.\n\t\tQuery().\n\t\tWhere(machine.MachineIdEQ(machineID)).\n\t\tOnly(ctx)\n\tif err != nil {\n\t\tc.Log.Warningf(\"QueryMachineByID : %s\", err)\n\t\treturn &ent.Machine{}, fmt.Errorf(\"user '%s': %w\", machineID, UserNotExists)\n\t}\n\n\treturn machine, nil\n}\n","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/crowdsecurity/crowdsec/blob/909b5157986a2b2c2163300fdaef5ed01289f7d2/pkg/database/machines.go#L93-L129","documentation":"CreateMachine wraps the sentinel InsertFail (errors.New(\"failed insert query\")) when the ent client's machine .Save() returns an error. The row could not be written to the machines table. The original ent/SQL error is logged as a warning but replaced here, so check the LAPI logs for the underlying cause.","triggerScenarios":"Client.CreateMachine calls c.Ent.Machine.Create()...Save(ctx) and the insert fails: DB connection down, schema mismatch (run `cscli migration` / version drift), unique constraint from a concurrent insert of the same machine_id, or disk full on the SQLite file.","commonSituations":"SQLite database file locked by another process or deleted/corrupted; LAPI started before the DB schema was migrated; MySQL/Postgres credentials changed after install; two LAPI instances writing concurrently.","solutions":["Inspect the LAPI log line 'CreateMachine : <err>' for the underlying ent/SQL error","Verify database connectivity and credentials in /etc/crowdsec/config.yaml (db_config)","Run `cscli migration` / upgrade steps so the schema matches the binary version","If SQLite, check the file exists, is writable, and no stale lock: `ls -l /var/lib/crowdsec/data/crowdsec.db`","Retry the registration; if a unique constraint raced, the second attempt is a duplicate (see UserExists)"],"exampleFix":null,"handlingStrategy":"retry","validationCode":"// ping the DB before registering\nif err := db.Client.Database.SQLClient.Ping(); err != nil {\n    return fmt.Errorf(\"DB unavailable: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"if _, err := db.CreateMachine(ctx, id, pw, ip, valid, auth); err != nil {\n    if errors.Is(err, database.InsertFail) {\n        // check LAPI log 'CreateMachine : <err>' for the ent root cause, retry after fixing\n    }\n    return err\n}","preventionTips":["Ensure migrations ran for the installed version before accepting registrations","For SQLite, avoid multiple concurrent LAPI writers; use MySQL/Postgres at scale","Monitor disk space on the DB host","Keep db_config credentials/host in config.yaml current after infra changes"],"tags":["database","insert-failed","sqlite","crowdsec","lapi"],"backgroundTag":"database-write-failed","analyzedSha":"909b5157986a2b2c2163300fdaef5ed01289f7d2","analyzedAt":"2026-09-06T12:27:26.012Z","contentChangedAt":"2026-09-06T12:27:26.012Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}