Title: Updating Rows: Documented syntax doesn't produce expected result for MySQL and Sqlite · Issue #3697 · sqlc-dev/sqlc · GitHub
Open Graph Title: Updating Rows: Documented syntax doesn't produce expected result for MySQL and Sqlite · Issue #3697 · sqlc-dev/sqlc
X Title: Updating Rows: Documented syntax doesn't produce expected result for MySQL and Sqlite · Issue #3697 · sqlc-dev/sqlc
Description: Version 1.27.0 What happened? High Level The documentation for updating rows appears to be incomplete, resulting in an error for Sqlite and a silent-but-incorrect generation for MySQL. It appears that the $1 placeholders used in the docu...
Open Graph Description: Version 1.27.0 What happened? High Level The documentation for updating rows appears to be incomplete, resulting in an error for Sqlite and a silent-but-incorrect generation for MySQL. It appears t...
X Description: Version 1.27.0 What happened? High Level The documentation for updating rows appears to be incomplete, resulting in an error for Sqlite and a silent-but-incorrect generation for MySQL. It appears t...
Opengraph URL: https://github.com/sqlc-dev/sqlc/issues/3697
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Updating Rows: Documented syntax doesn't produce expected result for MySQL and Sqlite","articleBody":"### Version\n\n1.27.0\n\n### What happened?\n\n# High Level\r\nThe [documentation](https://github.com/sqlc-dev/sqlc/blob/main/docs/howto/update.md) for updating rows appears to be incomplete, resulting in an error for Sqlite and a silent-but-incorrect generation for MySQL. It appears that the `$1` placeholders used in the documentation are not valid for these two backends (but is valid for PostgreSQL,) and that `?` should be used for the Sqlite and MySQL backends.\r\n\r\n# Suggested resolution\r\nUpdate the documentation to reflect differences in SQL dialects. As someone unfamiliar with PostgreSQL it wasn't clear if the use of `$1` was specific to sqlc or not, so having a note and examples using the other backends would be great.\r\n\r\nThe silent failure to generate expected MySQL outputs (see code block at bottom) is an issue that isn't as clear to fix. The user will quickly see that the generated functions don't behave as expected, but there's no output or indication as to what went wrong. One place I could see this potentially occurring is a codebase that uses PostgreSQL and then adds the MySQL backend using the same queries - it's possible that they are \"compatible\" enough to silently pass through. Perhaps `sqlc` could emit a warning if it detects the `$#` syntax within a query targeting MySQL? \r\n\r\nIf possible it might also be helpful to have nicer error messages that catch this case for the Sqlite version too, indicating that the syntax might be using the wrong dialect; I'm not sure how viable such a warning would be to implement.\r\n\r\n# What I did\r\n\r\nsqcl 1.27.0, generating go code\r\n\r\nWhen following the [documentation](https://github.com/sqlc-dev/sqlc/blob/main/docs/howto/update.md) for updating rows, I ran into two issues. When generating for sqlite, the `$1` syntax demonstrated in the documentation's `UPDATE authors SET bio = $1;` results in the error below. When generating for mysql, the syntax does not result in an error but does not generate the expected go code.\r\n\r\n## Sqlite Reproduction\r\n\r\n1. Using the attached configuration, comment out the mysql portion of the yaml\r\n2. Run `sqlc generate`\r\n3. Note the error output attached below\r\n4. Edit the `query.sql` to only include the `UpdateExampleGood` query\r\n5. Run `sqlc generate`\r\n6. Note no error output\r\n7. Note that `sqlite/query.sql.go` contains the expected UpdateExampleParams struct\r\n\r\n## MySQL Reproduction\r\n\r\n1. Using the attached configuration, comment out the sqlite portion of the yaml\r\n2. Revert the `query.sql` to the attached version if it was edited\r\n3. Run `sqlc generate`\r\n4. Note no error output\r\n5. Note that `mysql/query.sql.go` contains two Query functions\r\n - `UpdateExample` does not have an associated Params struct\r\n - `UpdateExampleGood` does have the expected Params struct\r\n\r\n\r\n## Example MySQL output with expected and unexpected generated functions\r\n\r\n```go\r\n// Code generated by sqlc. DO NOT EDIT.\r\n// versions:\r\n// sqlc v1.27.0\r\n// source: query.sql\r\n\r\npackage example_issue\r\n\r\nimport (\r\n\t\"context\"\r\n)\r\n\r\nconst updateExample = `-- name: UpdateExample :exec\r\nUPDATE Example SET name = $2 WHERE id = $1\r\n`\r\n\r\n/ / NOTE: No Params struct for the `$` syntax, but the function is generated anyway\r\nfunc (q *Queries) UpdateExample(ctx context.Context) error {\r\n\t_, err := q.db.ExecContext(ctx, updateExample)\r\n\treturn err\r\n}\r\n\r\nconst updateExampleGood = `-- name: UpdateExampleGood :exec\r\nUPDATE Example SET name = ? WHERE id = ?\r\n`\r\n\r\ntype UpdateExampleGoodParams struct {\r\n\tName string\r\n\tID int32\r\n}\r\n\r\n// NOTE: Params struct is generated with the `?` syntax\r\nfunc (q *Queries) UpdateExampleGood(ctx context.Context, arg UpdateExampleGoodParams) error {\r\n\t_, err := q.db.ExecContext(ctx, updateExampleGood, arg.Name, arg.ID)\r\n\treturn err\r\n}\r\n\r\n\r\n```\n\n### Relevant log output\n\n```shell\n** Running `sqlc generate` with sqlite backend:\r\n\r\nline 2:26 no viable alternative at input 'UPDATE Example SET name = $'\r\n# package example_issue\r\nquery.sql:1:1: no viable alternative at input 'UPDATE Example SET name = $'\r\n\r\n** No log outputs for mysql backend\n```\n\n\n### Database schema\n\n```sql\nCREATE TABLE IF NOT EXISTS Example (\r\n id INTEGER NOT NULL,\r\n name TEXT NOT NULL\r\n);\n```\n\n\n### SQL queries\n\n```sql\n-- This query fails to generate for sqlite, and doesn't generate Params for mysql\r\n-- name: UpdateExample :exec\r\nUPDATE Example SET name = $2 WHERE id = $1;\r\n\r\n-- This version produces the expected output for both\r\n-- name: UpdateExampleGood :exec\r\nUPDATE Example SET name = ? WHERE id = ?;\n```\n\n\n### Configuration\n\n```yaml\nversion: \"2\"\r\nsql:\r\n - engine: \"mysql\"\r\n queries: \"query.sql\"\r\n schema: \"schema.sql\"\r\n gen:\r\n go:\r\n package: \"example_issue\"\r\n out: \"mysql\" \r\n - engine: \"sqlite\"\r\n queries: \"query.sql\"\r\n schema: \"schema.sql\"\r\n gen:\r\n go:\r\n package: \"example_issue\"\r\n out: \"sqlite\"\r\n \r\n \r\n # NOTE: Included for completeness; to use you need to remove the `UpdateExampleGood` query\r\n # since it doesn't have compatible syntax but does work with the documented $1, $2 syntax \r\n # - engine: \"postgresql\"\r\n # queries: \"query.sql\"\r\n # schema: \"schema.sql\"\r\n # gen:\r\n # go:\r\n # package: \"example_issue\"\r\n # out: \"postgresql\"\n```\n\n\n### Playground URL\n\n_No response_\n\n### What operating system are you using?\n\nLinux\n\n### What database engines are you using?\n\nMySQL, SQLite\n\n### What type of code are you generating?\n\nGo","author":{"url":"https://github.com/ookjosh","@type":"Person","name":"ookjosh"},"datePublished":"2024-11-09T03:03:02.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/3697/sqlc/issues/3697"}
| 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:726083cb-ede7-0798-e093-272fe1051ee0 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9C7E:18EF1B:8C66F49:C33D07F:6A50726F |
| html-safe-nonce | 4628fc1ef9e9d76b4ffb5200de76022ce2930bacc8c6fc19ddbcd5a3f96f2754 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QzdFOjE4RUYxQjo4QzY2RjQ5OkMzM0QwN0Y6NkE1MDcyNkYiLCJ2aXNpdG9yX2lkIjoiMzM2NzQwMzI5NDEzMzYxMzE2NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 37229c5dc09328a44ececee25d18575d86ad7085382dad50d6d4dec010a8f5f4 |
| hovercard-subject-tag | issue:2645528808 |
| 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/3697/issue_layout |
| twitter:image | https://opengraph.githubassets.com/af041e43d1d0b1558ffb3d5de2045b25fc1987e00b1188e4826a0535241499de/sqlc-dev/sqlc/issues/3697 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/af041e43d1d0b1558ffb3d5de2045b25fc1987e00b1188e4826a0535241499de/sqlc-dev/sqlc/issues/3697 |
| og:image:alt | Version 1.27.0 What happened? High Level The documentation for updating rows appears to be incomplete, resulting in an error for Sqlite and a silent-but-incorrect generation for MySQL. It appears t... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ookjosh |
| 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 | 7ac0ad2f2c7e4b9056617355fd9e33e22b0c8df9 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width