{"record":{"id":"ec54bfa3f0916d57","repo":"apache/beam","slug":"failed-to-prepare-query-v","errorCode":null,"errorMessage":"failed to prepare query: %v","messagePattern":"failed to prepare query: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/io/databaseio/database.go","lineNumber":80,"sourceCode":"\tDriver string `json:\"driver\"`\n\t// Project is the project\n\tDsn string `json:\"dsn\"`\n\t// Table is the table identifier.\n\tQuery string `json:\"query\"`\n\t// Type is the encoded schema type.\n\tType beam.EncodedType `json:\"type\"`\n}\n\nfunc (f *queryFn) ProcessElement(ctx context.Context, _ []byte, emit func(beam.X)) error {\n\t//TODO move DB Open and Close to Setup and Teardown methods or StartBundle and FinishBundle\n\tdb, err := sql.Open(f.Driver, f.Dsn)\n\tif err != nil {\n\t\treturn errors.Wrapf(err, \"failed to open database: %v\", f.Driver)\n\t}\n\tdefer db.Close()\n\tstatement, err := db.PrepareContext(ctx, f.Query)\n\tif err != nil {\n\t\treturn errors.Wrapf(err, \"failed to prepare query: %v\", f.Query)\n\t}\n\tdefer statement.Close()\n\trows, err := statement.QueryContext(ctx)\n\tif err != nil {\n\t\treturn errors.Wrapf(err, \"failed to run query: %v\", f.Query)\n\t}\n\tdefer rows.Close()\n\tvar mapper rowMapper\n\tvar columns []string\n\tfor rows.Next() {\n\t\treflectRow := reflect.New(f.Type.T)\n\t\trow := reflectRow.Interface() // row : *T\n\t\tif mapper == nil {\n\t\t\tcolumns, err = rows.Columns()\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\tcolumnsTypes, _ := rows.ColumnTypes()","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/io/databaseio/database.go#L62-L98","documentation":"After opening the DB, queryFn.ProcessElement prepares the configured SQL statement with db.PrepareContext(ctx, f.Query). Failure is wrapped as \"failed to prepare query: %v\" with the query text. Prepare asks the server to parse/plan the statement, so this fires on SQL syntax errors, bad placeholders, or nonexistent objects during planning.","triggerScenarios":"db.PrepareContext returns an error: invalid SQL syntax for the target dialect, wrong parameter placeholder style (? vs $1), referencing a missing table/column at prepare time, or connection dropped before prepare.","commonSituations":"Queries copied between MySQL and Postgres with incompatible placeholder syntax; typo'd table/column names; schema migrations renaming columns; the connection dying due to idle timeouts before prepare.","solutions":["Copy the query text from the error and run it directly against the database (with placeholders filled) to see the precise SQL error.","Match the placeholder style to the driver ($1... for Postgres, ? for MySQL/sqlite).","Verify all referenced tables/columns exist in the target schema after migrations.","Check the DSN/database name — preparing against the wrong database or schema fails when objects are missing."],"exampleFix":"// before\nf.Query = \"SELECT * FROM users WHERE id = ?\" // Postgres driver\n\n// after\nf.Query = \"SELECT * FROM users WHERE id = $1\"","handlingStrategy":"validation","validationCode":"stmt, err := db.PrepareContext(ctx, query)\nif err != nil { return fmt.Errorf(\"prepare failed: %w (check syntax and placeholder style for %s)\", err, driver) }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use the correct placeholder style per driver ($1 for Postgres, ? for MySQL/SQLite).","Run queries against a staging schema to catch renames after migrations.","Keep queries as reviewed constants/vars, not ad-hoc string concatenation."],"tags":["database","sql","query","go"],"backgroundTag":"sql-query-failed","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}