問(wèn)題描述
我需要一種有效的方法來(lái)禁止 Laravel 自動(dòng)識(shí)別我要插入數(shù)據(jù)的表的主鍵.
I need an effective way to disable Laravel from auto incriminating the primary key of the table which I am going to insert data in.
為什么?我不檢查數(shù)據(jù)庫(kù)和插入的數(shù)據(jù)之間是否有任何重復(fù),所以如果有任何重復(fù),我只是在 try-catch 塊中處理它.
Why? I don't check if there is any duplication between the DB and the inserted data so if there was any duplication I just handles it in a try-catch block.
問(wèn)題是如果有任何失敗,Laravel 會(huì)像我插入一行一樣計(jì)數(shù).所以 IDs 列不會(huì)按照這個(gè)順序 [1, 2, 3, etc],而是按照這個(gè) [1, 4, 8, 20, etc].
The problem is if there was any failure Laravel counts it like I have inserted a row. So IDs column is not going to be in this order [1, 2, 3, etc], but in this [1, 4, 8, 20, etc].
我搜索了很多關(guān)于這個(gè)問(wèn)題,我嘗試在類聲明后使用這一行:
I searched a lot about this issue and I have tried to use this line after the declaration of the class:
public $autoincrement = false;
還有
public $incrementing = false;
但它們不起作用.
我只想使用我的數(shù)據(jù)庫(kù)的 AI.不是 Laravel 的.
推薦答案
抱歉,大家耽誤了您的時(shí)間,問(wèn)題是,如果我們嘗試在 MYSQL 中保存任何記錄,無(wú)論它因任何原因返回成功還是失敗(例如在我的情況下重復(fù)),
Sorry for taking your time guys, The issue is if we tried to save any record in MYSQL whether it returned success or failure for any reason (like for duplication in my case),
MYSQL 計(jì)算自動(dòng)增量編號(hào)已被預(yù)訂,并且任何其他即將到來(lái)的記錄都不會(huì)占用它,因?yàn)榭赡芡瑫r(shí)在 DB 上有多個(gè)插入過(guò)程并等待知道自動(dòng)增量編號(hào)是否為預(yù)訂與否都會(huì)降低 MYSQL 的速度.
MYSQL counts that the auto increment number was booked and any other coming record is not going to take it because of there may be more than an insertion process on the DB in the same time and waiting to know if the auto incrementally number is booked or not will cost MYSQL its speed.
所以這不是 Laravel 的問(wèn)題,而是 MYSQL 的問(wèn)題.
So it is not a Laravel issue, its a MYSQL one.
我希望它可以幫助其他人.
I hope that it may help others.
謝謝大家...
這篇關(guān)于如何禁用 Laravel eloquent Auto Increment?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!