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

select last_insert_rowid() doesn't account for ROLLBACK on transaction #22

Open
pydanny opened this issue Oct 23, 2024 · 1 comment
Open

Comments

@pydanny
Copy link
Contributor

pydanny commented Oct 23, 2024

We probably don't want to use the SQLIte function last_insert_rowid() to get the last_pk. Per the docs on last_insert_rowid, this is expected behavior:

For the purposes of this routine, an INSERT is considered to be successful even if it is subsequently rolled back.


Get last rowid

SELECT last_insert_rowid();

0

Now do the transaction and check the row id:

BEGIN TRANSACTION;
INSERT INTO users (username) VALUES ('puppy');
SELECT last_insert_rowid();

1

Roll the transaction back and check the table and rowid:

ROLLBACK;
SELECT last_insert_rowid();

1

@pydanny
Copy link
Contributor Author

pydanny commented Oct 23, 2024

@audreyfeldroy's fix from yesterday:

BEGIN IMMEDIATE TRANSACTION;

INSERT INTO your_table (column1, column2, ...) VALUES (value1, value2, ...);
SELECT last_insert_rowid() AS last_inserted_id;

COMMIT;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant