Title: Select from views and `array_agg` · Issue #3071 · sqlc-dev/sqlc · GitHub
Open Graph Title: Select from views and `array_agg` · Issue #3071 · sqlc-dev/sqlc
X Title: Select from views and `array_agg` · Issue #3071 · sqlc-dev/sqlc
Description: Version 1.24.0 What happened? We have a number of views that are created by joining tables, grouping, and array_agging some columns. Sqlc generated the right code previously, but if you turn on database-backed query analysis then while t...
Open Graph Description: Version 1.24.0 What happened? We have a number of views that are created by joining tables, grouping, and array_agging some columns. Sqlc generated the right code previously, but if you turn on dat...
X Description: Version 1.24.0 What happened? We have a number of views that are created by joining tables, grouping, and array_agging some columns. Sqlc generated the right code previously, but if you turn on dat...
Opengraph URL: https://github.com/sqlc-dev/sqlc/issues/3071
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Select from views and `array_agg`","articleBody":"### Version\n\n1.24.0\n\n### What happened?\n\nWe have a number of views that are created by joining tables, grouping, and `array_agg`ing some columns. Sqlc generated the right code previously, but if you turn on database-backed query analysis then while the model for the view is still correct, select queries from the view don't return that model and instead return a different and also otherwise incorrect type.\r\n\r\nI.e. in the playground example below, \r\n```\r\ntype BooksWithAuthor struct {\r\n\tBooks []string\r\n\tID int64\r\n\tName string\r\n\tBio sql.NullString\r\n}\r\n```\r\nis correct, but the select query instead returns\r\n```\r\ntype GetAuthorBooksRow struct {\r\n\tBooks string\r\n\tID int64\r\n\tName string\r\n\tBio sql.NullString\r\n}\r\n```\r\nwhich is not only different, but also has an incorrect type for `Books`.\n\n### Relevant log output\n\n_No response_\n\n### Database schema\n\n```sql\nCREATE TABLE authors (\r\n id BIGSERIAL PRIMARY KEY,\r\n name text NOT NULL,\r\n bio text\r\n);\r\n\r\ncreate table books (\r\n id BIGSERIAL PRIMARY KEY,\r\n name text NOT NULL,\r\n author BIGSERIAL references authors(id)\r\n );\r\n \r\n create view books_with_author as \r\n \tselect array_agg(books.name)::text[] as books, authors.* from books\r\n join authors on authors.id = books.author\r\n group by authors.id;\n```\n\n\n### SQL queries\n\n```sql\n-- name: GetAuthorBooks :one\r\nSELECT * FROM books_with_author;\n```\n\n\n### Configuration\n\n```yaml\n{\r\n \"version\": \"2\",\r\n \"cloud\": {\r\n \t\"project\":\"\u003cPROJECT_ID\u003e\"\r\n },\r\n \"sql\": [{\r\n \"schema\": \"schema.sql\",\r\n \"queries\": \"query.sql\",\r\n \"engine\": \"postgresql\",\r\n \"database\" :{\r\n \t\"managed\": true\r\n },\r\n \"gen\": {\r\n \"go\": {\r\n \"out\": \"db\"\r\n }\r\n }\r\n }]\r\n}\n```\n\n\n### Playground URL\n\nhttps://play.sqlc.dev/p/9d855f57e257cbd5acd956f1bc9d5b1f55ada9604602b5acd1742dd49337e289\n\n### What operating system are you using?\n\nLinux\n\n### What database engines are you using?\n\nPostgreSQL\n\n### What type of code are you generating?\n\nGo","author":{"url":"https://github.com/mariusavos","@type":"Person","name":"mariusavos"},"datePublished":"2023-12-19T15:18:24.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/3071/sqlc/issues/3071"}
| 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:d852281e-09d9-ac44-e07b-6bd9f251e69b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EC80:13AD7A:54D9E0:70C6B2:6A504994 |
| html-safe-nonce | 504bd588784c1e4a39a9386ffaa05470aeb5cb60b7d235303b0d6d42ac4ebcb4 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQzgwOjEzQUQ3QTo1NEQ5RTA6NzBDNkIyOjZBNTA0OTk0IiwidmlzaXRvcl9pZCI6IjU1MzQwNTc5NDA5NzYxNTkxMjQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 1a0063d2bf81f4481cba231ea9f7784d1b62da86d9238b202735fe02b29139a7 |
| hovercard-subject-tag | issue:2048873947 |
| 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/3071/issue_layout |
| twitter:image | https://opengraph.githubassets.com/0c836c07adcd021a0e5d033570d8c78db84078f5af947d3e8b6940bb9494cd7d/sqlc-dev/sqlc/issues/3071 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/0c836c07adcd021a0e5d033570d8c78db84078f5af947d3e8b6940bb9494cd7d/sqlc-dev/sqlc/issues/3071 |
| og:image:alt | Version 1.24.0 What happened? We have a number of views that are created by joining tables, grouping, and array_agging some columns. Sqlc generated the right code previously, but if you turn on dat... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mariusavos |
| hostname | github.com |
| expected-hostname | github.com |
| None | d6dc8294eb500fa36bbc57472d61fe87c522f9c3c1d64f70f4926f66a66a7efb |
| 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 | 5741a45fe07e5f377d0d4c524a92e3056acc51bb |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width