{
  "id": "go/sql-rows-err-lastinsertid",
  "signature": "sql: Rows are closed, cannot call LastInsertId on result",
  "signature_zh": "sql：行已关闭，无法对结果调用 LastInsertId",
  "regex": "sql: Rows are closed, cannot call LastInsertId on result",
  "domain": "go",
  "category": "data_error",
  "subcategory": null,
  "root_cause": "Calling Result.LastInsertId() on a sql.Result from an INSERT that used a SELECT or RETURNING clause, or after rows have been consumed.",
  "root_cause_type": "generic",
  "root_cause_zh": "对来自使用 SELECT 或 RETURNING 子句的 INSERT 的 sql.Result 调用 Result.LastInsertId()，或在行已被消耗后调用。",
  "versions": [
    {
      "version": "Go 1.15",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Go 1.16",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Go 1.17",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Go 1.18",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Go 1.19",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Go 1.20",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Go 1.21",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Go 1.22",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Go 1.23",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Rows.Close() closes the result set, making LastInsertId unavailable; order is wrong.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "db.Exec() returns a Result that supports LastInsertId, but if the INSERT has a RETURNING clause, it still fails.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "The result is tied to the original query; reconnecting doesn't recover the last insert ID.",
      "fail_rate": 0.95,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Use db.Exec() for INSERT statements without RETURNING; use db.QueryRow() with RETURNING and scan into a variable",
      "success_rate": 0.9,
      "how": "Use db.Exec() for INSERT statements without RETURNING; use db.QueryRow() with RETURNING and scan into a variable",
      "condition": "",
      "sources": []
    },
    {
      "action": "Access LastInsertId immediately after Exec() before any rows iteration",
      "success_rate": 0.85,
      "how": "Access LastInsertId immediately after Exec() before any rows iteration",
      "condition": "",
      "sources": []
    },
    {
      "action": "For PostgreSQL with RETURNING, use a separate SELECT currval() or lastval() query",
      "success_rate": 0.8,
      "how": "For PostgreSQL with RETURNING, use a separate SELECT currval() or lastval() query",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Use db.Exec() for INSERT statements without RETURNING; use db.QueryRow() with RETURNING and scan into a variable",
    "Access LastInsertId immediately after Exec() before any rows iteration",
    "For PostgreSQL with RETURNING, use a separate SELECT currval() or lastval() query"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://pkg.go.dev/database/sql#Result.LastInsertId",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.85,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-01-10",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}