Ready to get started?
Join Adocasts Plus for $8/mo, or sign into an existing Adocasts Plus account, to get access to all of our lessons.
Building with AdonisJS & Inertia #9.3
Editing & Deleting Course Lessons
We'll add the ability to edit and delete lessons from a course's module. When editing, we'll also decrement the order field for all lessons within the module after the lesson being deleted.
- Created by
- @tomgobich
- Published
Join the Discussion 5 comments
-
Sorry for all the questions. I am getting a lock timeout error when I try to delete. I copied your destroy_lesson code from the repo to make sure our files matched and still get it. I get it trying to delete any lesson. I built my DB fresh and reseeded, just in case. If I remove the await decrement lesson I can delete lessons just fine, just for some reason the lessons are locked when trying to increment:
ERROR (5078): updatelessonssetorder=order- 1 where (`module_id` = 1 andorder> 1) and (`organization_id` = 1) - Lock wait timeout exceeded; try restarting transaction.The query looks right, it seems to be trying to update the right data. Any ideas?
1-
Responding to aaron-ford
No worries at all! It's been a long while since I've used MySQL, so my platform-specific knowledge here will be limited. But, if it works fine without the decrement then I'd imagine you most likely have a stuck lock or transaction on one of your lessons. Something not directly related to the decrement, but rather blocking the decrement from succeeding.
This may or may not be code-related. It could be a lock added by a client application or CLI using your MySQL table. It could also be another transaction touching one of the lesson rows that was never committed or rolled back. Here is a StackOverflow thread that discussed some query options to dig into what might be locking things up.
I can try and get MySQL set up and test it out this evening after work to ensure it isn't a code issue specific to MySQL.
0-
Responding to tomgobich
Alrighty, so I took a look into it. I was also getting the error with MySQL, and it was indeed a code issue! I accidentally missed binding the organization to the transaction, which caused the decrement to run outside the confinement of the transaction - hence the lock block.
Below is the updated code that fixes this issue! Terribly sorry about the miss here! I'll get a note about this added into the lesson.
export default class DestroyLesson { static async handle({ organization, id }: Params) { const lesson = await organization.related('lessons').query().where({ id }).firstOrFail() await db.transaction(async (trx) => { lesson.useTransaction(trx) organization.useTransaction(trx) // 👈 await lesson.delete() await organization .related('lessons') .query() .where('moduleId', lesson.moduleId) .where('order', '>', lesson.order) .decrement('order') }) return lesson } }Copied!0-
Responding to tomgobich
-
Responding to aaron-ford
-
-
-
-