dshakcsq64956 2018-03-11 11:17
浏览 146

Laravel firstOrNew为相同数据创建重复条目

I have this specific code from my application:

if ($request->filled('jawaban')) {
                $jawaban = JawabanUserM::where('user_id', $user->id)
                    ->where('paket_id', $kuis['paket_id'])->firstOrNew(['soal_id'=>$kuis['curr_id']]);
                if($jawaban->jawaban_id != $request->jawaban) {
                    $jawaban->user_id = $user->id;
                    $jawaban->paket_id = $kuis['paket_id'];
                    $jawaban->soal_id = $kuis['curr_id'];
                    $jawaban->jawaban_id = $request->jawaban;
                    $jawaban->save();
                }
        }

it basicly insert answer from user to table JawabanUserM

I run this application on deployment. using laravel 5.5, mysql 5.5.58, nginx 1.12.2, php-fpm 7.0.27.

with 800-1000++ average user at a time. using InnoDB engine.

when i check around i found some duplicate records. i check using this sql :

SELECT
    id, soal_id, user_id, jawaban_id, paket_id, COUNT(*)
FROM
    jawabanuserm
GROUP BY
    soal_id, user_id, jawaban_id, paket_id
HAVING 
    COUNT(*) > 1
ORDER BY COUNT(*) DESC

the result was :

id | soal_id | user_id | jawaban_id | paket_id | COUNT(*)
4702 | 117 | 100 | 401 | 6 | 6
4694 | 111 | 100 | 401 | 6 | 5
15080 | 110 | 1300 | 1077 | 7 | 5
8097 | 186 | 435 | 686 | 6 | 4
4679 | 97 | 100 | 401 | 6 | 4
....

Any advice to workaround this problem and why this is happening?

at first i strongly believe this is caused by bad communicating between process. i have ask to my colleague they suggest using procedure, but it will impact the performance i think.

  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!
    • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
    • ¥15 求daily translation(DT)偏差订正方法的代码
    • ¥15 js调用html页面需要隐藏某个按钮
    • ¥15 ads仿真结果在圆图上是怎么读数的
    • ¥20 Cotex M3的调试和程序执行方式是什么样的?
    • ¥20 java项目连接sqlserver时报ssl相关错误
    • ¥15 一道python难题3
    • ¥15 牛顿斯科特系数表表示