avoiding conflicts in mySQL
Hi im trying to avoid conflicts in my app by checking the 'lastModified' column on the database against the client.
But I also need to add successfully updated records to a status log table to record the history of changes. Because I'm using mySQL there's no way of returning specific rows effected. Is there a common approach for this?
12 Replies
I think this is the correct approach for this
Thanks @Angelelz ! I had to write a lot more to also insert successful updates to the statusLog table. I added a new column used to write a unique transaction id to the record (idtx) which can be read to see which records were updated by the transaction
would be a lot simpler if mysql returned ids 😂
Doesn't mysql return the insertedId?
Or you need it in the update statement?
I kinda remember there was an updatedId as well
Let me check
Nope, you're right
thanks! yeah im new to db stuff an on the drizzle docs there is no built in way to do update returns:
https://orm.drizzle.team/docs/update#update-with-returning
SQL Update - Drizzle ORM
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
InsertId is only available on the insert
Update doesn't have anything, only
affectedRows
which is just a numberyeahhh i think i used that elsewhere in my codebase, to get the auto increment id of an insert
Drizzle doesn't have it because mysql doesn't have it
Let me check something else real quick
Yeah i suspected so! Also planetscale doesnt do procedures so thats another avenue blocked
Do you have an index on
lastModified
?no i dont think so
I should add one right, since im using the column in a WHERE clause?
Not because of that, because you're already using the id in the where
So not because of that, you don't need it
There is a way of doing an update and getting the id back but it doesn't work for you, because of your extra lastModified in the where
This returns:
and updates the record to 'jjj'
But if you try to do this, you'll update all the records with those ids regardless of the lastModified
You have the only solution, I think
Ahh I see, thats a handy return object
yeah, i've been trying to find a solution all weekend haha
good to have someone who knows what theyre doing look over it though so thanks!
gpt only gets you so far 🤖
in fact gpt recommended making 'temporary tables' but i think thats overkill, plus i'm not 100% sure it solves the issue