7 months ago

Attach and detaching to Quickly Results in a Constraint Violation

Posted 7 months ago by ctyler

Hello All, I am having an interesting issue in Internet Explorer 11. I have an application where student can enroll and un-enroll themselves in class. This is done via attach and detach statement:


$enrollment->users()->attach($user->id, ['enroll_code' => $enrollmentId]);

Un-enrolling $user->enrollments()->detach($enrollment->id);

If the user is enrolled an un-enroll button is shown

If the user is not enrolled an enroll now button is shown.

I have a unique index set up on user_id and the course_id on the enrollment table.

Now, in Chrome, Firefox, and Edge I can click this button as fast as I can without issue. However, in IE 11, if I do that I get a constraint violation:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '91-6' for key 'PRIMARY' (SQL: insert into `enrollment_user` (`created_at`, `enroll_code`, `enrollment_id`, `updated_at`, `user_id`) values (2020-02-09 10:40:07, 916Q1FGO, 91, 2020-02-09 10:40:07, 6))

I tried adding javascript to the page to make sure the buttons (both enroll and un-enroll) are disabled until the page loads via:

setting the buttons to disabled and then adding:


I still get the error.

In the Controller is there anyway to make sure the query has completed before calling:

return back();

or is there another way to handle this?

Please sign in or create an account to participate in this conversation.