Title: LEFT JOIN can result in unsupported Scan · Issue #374 · sqlc-dev/sqlc · GitHub
Open Graph Title: LEFT JOIN can result in unsupported Scan · Issue #374 · sqlc-dev/sqlc
X Title: LEFT JOIN can result in unsupported Scan · Issue #374 · sqlc-dev/sqlc
Description: When using LEFT JOINs with, for example, header/detail tables, the absence of any detail rows can cause the following: sql: Scan error on column index 3, name \"purchase_order_id\": unsupported Scan, storing driver.Value type
Open Graph Description: When using LEFT JOINs with, for example, header/detail tables, the absence of any detail rows can cause the following: sql: Scan error on column index 3, name \"purchase_order_id\": unsupported Sca...
X Description: When using LEFT JOINs with, for example, header/detail tables, the absence of any detail rows can cause the following: sql: Scan error on column index 3, name \"purchase_order_id\": unsup...
Opengraph URL: https://github.com/sqlc-dev/sqlc/issues/374
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"LEFT JOIN can result in unsupported Scan ","articleBody":"When using `LEFT JOIN`s with, for example, header/detail tables, the absence of any detail rows can cause the following:\r\n```\r\nsql: Scan error on column index 3, name \\\"purchase_order_id\\\": unsupported Scan, storing driver.Value type \u003cnil> into type *string\r\n```\r\n\r\nGiven the following schema...\r\n```sql\r\nCREATE TABLE purchase_order (\r\n\tid TEXT NOT NULL PRIMARY KEY,\r\n\tcreated TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),\r\n\tupdated TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now()\r\n);\r\n\r\nCREATE TABLE purchase_order_transaction (\r\n\tpurchase_order_id TEXT NOT NULL REFERENCES purchase_order(id),\r\n\tdescription TEXT NOT NULL,\r\n\tinvoice_total INTEGER NOT NULL,\r\n\tcreated TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now()\r\n);\r\n```\r\n\r\nand the following query...\r\n```sql\r\n-- name: SelectPurchaseOrder :many\r\nSELECT po.*, tx.*\r\nFROM purchase_order AS po\r\nLEFT JOIN purchase_order_transaction AS tx\r\n\tON tx.purchase_order_id = po.id\r\nWHERE po.id = $1\r\nORDER BY tx.created;\r\n```\r\n\r\n`sqlc` produces the following code:\r\n```go\r\nconst selectPurchaseOrder = `-- name: SelectPurchaseOrder :many\r\nSELECT po.id, po.created, po.updated, tx.purchase_order_id, tx.description, tx.invoice_total, tx.created\r\nFROM purchase_order AS po\r\nLEFT JOIN purchase_order_transaction AS tx\r\n\tON tx.purchase_order_id = po.id\r\nWHERE po.id = $1\r\nORDER BY tx.created\r\n`\r\n\r\ntype SelectPurchaseOrderRow struct {\r\n\tID string\r\n\tCreated time.Time\r\n\tUpdated time.Time\r\n\tPurchaseOrderID string\r\n\tDescription string\r\n\tInvoiceTotal int32\r\n\tCreated_2 time.Time\r\n}\r\n\r\nfunc (q *Queries) SelectPurchaseOrder(ctx context.Context, id string) ([]SelectPurchaseOrderRow, error) {\r\n\trows, err := q.db.QueryContext(ctx, selectPurchaseOrder, id)\r\n\tif err != nil {\r\n\t\treturn nil, err\r\n\t}\r\n\tdefer rows.Close()\r\n\tvar items []SelectPurchaseOrderRow\r\n\tfor rows.Next() {\r\n\t\tvar i SelectPurchaseOrderRow\r\n\t\tif err := rows.Scan(\r\n\t\t\t&i.ID,\r\n\t\t\t&i.Created,\r\n\t\t\t&i.Updated,\r\n\t\t\t&i.PurchaseOrderID,\r\n\t\t\t&i.Description,\r\n\t\t\t&i.InvoiceTotal,\r\n\t\t\t&i.Created_2,\r\n\t\t); err != nil {\r\n\t\t\treturn nil, err\r\n\t\t}\r\n\t\titems = append(items, i)\r\n\t}\r\n\tif err := rows.Close(); err != nil {\r\n\t\treturn nil, err\r\n\t}\r\n\tif err := rows.Err(); err != nil {\r\n\t\treturn nil, err\r\n\t}\r\n\treturn items, nil\r\n}\r\n```\r\n\r\nIt is my thinking that the `PurchaseOrderID`, `Description`, `InvoiceTotal`, and `Created_2` fields on `SelectPurchaseOrderRow` should be nullable to account for the absence of any `purchase_order_transaction` rows.","author":{"url":"https://github.com/Etomyutikos","@type":"Person","name":"Etomyutikos"},"datePublished":"2020-03-04T20:09:54Z","url":"https://github.com/sqlc-dev/sqlc/issues/374"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:48dcb384-69b4-a540-c724-e5c20a99793e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B2FA:17FB7:1742203:1D6FDB6:6B0174B2 |
| html-safe-nonce | 2a68cfe506d5014bad09517179579444b22a778eb17096549dbb8b9377597753 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMkZBOjE3RkI3OjE3NDIyMDM6MUQ2RkRCNjo2QjAxNzRCMiIsInZpc2l0b3JfaWQiOiI0NTQxMTA0MTcyNjUyNjU1Nzk0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 5c6912257c2daf6acd681642ec26b308945fb43ddec974778d1bd0fbf9b39380 |
| hovercard-subject-tag | issue:575733561 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/sqlc-dev/sqlc/374/issue_layout |
| twitter:image | https://opengraph.githubassets.com/9be95162c8dd18385441d155a8d5cb7086a4ea0cc01e67b4ee9cedc30c55827e/sqlc-dev/sqlc/issues/374 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/9be95162c8dd18385441d155a8d5cb7086a4ea0cc01e67b4ee9cedc30c55827e/sqlc-dev/sqlc/issues/374 |
| og:image:alt | When using LEFT JOINs with, for example, header/detail tables, the absence of any detail rows can cause the following: sql: Scan error on column index 3, name \"purchase_order_id\": unsupported Sca... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Etomyutikos |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/sqlc-dev/sqlc git https://github.com/sqlc-dev/sqlc.git |
| octolytics-dimension-user_id | 136738596 |
| octolytics-dimension-user_login | sqlc-dev |
| octolytics-dimension-repository_id | 193160679 |
| octolytics-dimension-repository_nwo | sqlc-dev/sqlc |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 193160679 |
| octolytics-dimension-repository_network_root_nwo | sqlc-dev/sqlc |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width