Title: PostgreSQL arrays can have NULL items · Issue #185 · sqlc-dev/sqlc · GitHub
Open Graph Title: PostgreSQL arrays can have NULL items · Issue #185 · sqlc-dev/sqlc
X Title: PostgreSQL arrays can have NULL items · Issue #185 · sqlc-dev/sqlc
Description: Given the following schema and query: CREATE TABLE foo ( id text NOT NULL, tags text[] ); -- name: ListFoo :many SELECT * FROM foo; sqlc will generate the following code: // Code generated by sqlc. DO NOT EDIT. package main import ( "con...
Open Graph Description: Given the following schema and query: CREATE TABLE foo ( id text NOT NULL, tags text[] ); -- name: ListFoo :many SELECT * FROM foo; sqlc will generate the following code: // Code generated by sqlc....
X Description: Given the following schema and query: CREATE TABLE foo ( id text NOT NULL, tags text[] ); -- name: ListFoo :many SELECT * FROM foo; sqlc will generate the following code: // Code generated by sqlc....
Opengraph URL: https://github.com/sqlc-dev/sqlc/issues/185
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"PostgreSQL arrays can have NULL items","articleBody":"Given the following schema and query: \r\n\r\n```sql\r\nCREATE TABLE foo (\r\n id text NOT NULL,\r\n tags text[]\r\n);\r\n\r\n-- name: ListFoo :many\r\nSELECT * FROM foo;\r\n```\r\n\r\nsqlc will generate the following code:\r\n\r\n```go\r\n// Code generated by sqlc. DO NOT EDIT.\r\n\r\npackage main\r\n\r\nimport (\r\n\t\"context\"\r\n\t\"database/sql\"\r\n\r\n\t\"github.com/lib/pq\"\r\n)\r\n\r\ntype Foo struct {\r\n\tID sql.NullString\r\n\tTags []string\r\n}\r\n\r\nconst listFoo = `-- name: ListFoo :many\r\nSELECT id, tags FROM foo\r\n`\r\n\r\nfunc (q *Queries) ListFoo(ctx context.Context) ([]Foo, error) {\r\n\trows, err := q.db.QueryContext(ctx, listFoo)\r\n\tif err != nil {\r\n\t\treturn nil, err\r\n\t}\r\n\tdefer rows.Close()\r\n\tvar items []Foo\r\n\tfor rows.Next() {\r\n\t\tvar i Foo\r\n\t\tif err := rows.Scan(&i.ID, pq.Array(&i.Tags)); 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\ntype DBTX interface {\r\n\tExecContext(context.Context, string, ...interface{}) (sql.Result, error)\r\n\tPrepareContext(context.Context, string) (*sql.Stmt, error)\r\n\tQueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)\r\n\tQueryRowContext(context.Context, string, ...interface{}) *sql.Row\r\n}\r\n\r\nfunc New(db DBTX) *Queries {\r\n\treturn &Queries{db: db}\r\n}\r\n\r\ntype Queries struct {\r\n\tdb DBTX\r\n}\r\n\r\nfunc (q *Queries) WithTx(tx *sql.Tx) *Queries {\r\n\treturn &Queries{\r\n\t\tdb: tx,\r\n\t}\r\n}\r\n```\r\n\r\nWith the following two records inserted into the `foo` table, a call `ListFoos` succeeds.\r\n\r\n```sql\r\nINSERT INTO foo (id, tags) VALUES ('one', NULL);\r\nINSERT INTO foo (id, tags) VALUES ('two', array['tag']);\r\n```\r\n\r\nHowever, if a NULL item exists in the `tags` array, an error will be returned.\r\n\r\n```sql\r\nINSERT INTO foo (id, tags) VALUES ('one', array[NULL]);\r\n```\r\n\r\n```\r\nsql: Scan error on column index 1, name \"tags\": pq: parsing array element index 0: cannot convert nil to string\r\n```\r\n\r\nThat's not a problem, right? We'll just mark the `tags` column as not null. Sadly, that just means that the column must contain an array value. The array itself can still contain NULL values. It does not appear to be easy to prevent NULL values in arrays. This was the [only solution](Declare a column of type 'not-null-string' array in postgresql - Stack Overflow) I could find, and it's pretty gross. \r\n\r\nNote that `pq.StringArray` also does not support NULL array values.","author":{"url":"https://github.com/kyleconroy","@type":"Person","name":"kyleconroy"},"datePublished":"2019-12-16T22:05:05Z","url":"https://github.com/sqlc-dev/sqlc/issues/185"}
| 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:eb0a9ae9-5c62-b56e-2a96-77cfb22b5b8e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 93EA:6365:1F2A257:29915C9:6B017A6A |
| html-safe-nonce | 1c3641cf2c730c07e8971531dd8c665a95a16075addc7c6763bda2a6f552a0e4 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5M0VBOjYzNjU6MUYyQTI1NzoyOTkxNUM5OjZCMDE3QTZBIiwidmlzaXRvcl9pZCI6IjU4OTkxMjIzNTcxOTIxOTQ2NjYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | c183e285f6331f96bda27c9045a9c3c4610c05a29462436a0f13acd94e35a3c6 |
| hovercard-subject-tag | issue:538693220 |
| 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/185/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5ba2264b245ec08c78c3faee28cadc9f307c433e8533a0bd16bfcf1911caa451/sqlc-dev/sqlc/issues/185 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5ba2264b245ec08c78c3faee28cadc9f307c433e8533a0bd16bfcf1911caa451/sqlc-dev/sqlc/issues/185 |
| og:image:alt | Given the following schema and query: CREATE TABLE foo ( id text NOT NULL, tags text[] ); -- name: ListFoo :many SELECT * FROM foo; sqlc will generate the following code: // Code generated by sqlc.... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | kyleconroy |
| 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