Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in the documentation for transactions? #3485

Open
tobyxdd opened this issue Jul 10, 2024 · 1 comment
Open

Error in the documentation for transactions? #3485

tobyxdd opened this issue Jul 10, 2024 · 1 comment
Labels
📚 mysql bug Something isn't working documentation Improvements or additions to documentation

Comments

@tobyxdd
Copy link

tobyxdd commented Jul 10, 2024

Version

1.26.0

What happened?

https://docs.sqlc.dev/en/latest/howto/transactions.html

In the given example, the code tries to select a row from records and increase the counter by 1 (in a transaction). According to the official MySQL manual:

If you query data and then insert or update related data within the same transaction, the regular SELECT statement does not give enough protection. Other transactions can update or delete the same rows you just queried. InnoDB supports two types of locking reads that offer extra safety - https://dev.mysql.com/doc/refman/8.4/en/innodb-locking-reads.html

So, shouldn't

-- name: GetRecord :one
SELECT * FROM records
WHERE id = $1;

be

-- name: GetRecord :one
SELECT * FROM records
WHERE id = $1
FOR UPDATE;

in this case?

@tobyxdd tobyxdd added bug Something isn't working triage New issues that hasn't been reviewed labels Jul 10, 2024
@kyleconroy kyleconroy added documentation Improvements or additions to documentation 📚 mysql and removed triage New issues that hasn't been reviewed labels Jul 16, 2024
@hungtcs
Copy link

hungtcs commented Jul 22, 2024

I don't think sqlc can handle locking very well because each query is independent, but locks and transactions are highly correlated.

I'm using a postgres database, and my current solution is to handle all operations that require locking as a function (or stored procedure), and then sqlc only makes the calls.

But although this simplifies the work of using sqlc, I need to write a large number of functions in the database.

If you have a better solution, please let me know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📚 mysql bug Something isn't working documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

3 participants