問題描述
我需要一種有效的方法來禁止 Laravel 自動識別我要插入數據的表的主鍵.
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.
為什么?我不檢查數據庫和插入的數據之間是否有任何重復,所以如果有任何重復,我只是在 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.
問題是如果有任何失敗,Laravel 會像我插入一行一樣計數.所以 IDs 列不會按照這個順序 [1, 2, 3, etc],而是按照這個 [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].
我搜索了很多關于這個問題,我嘗試在類聲明后使用這一行:
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;
但它們不起作用.
我只想使用我的數據庫的 AI.不是 Laravel 的.
推薦答案
抱歉,大家耽誤了您的時間,問題是,如果我們嘗試在 MYSQL 中保存任何記錄,無論它因任何原因返回成功還是失敗(例如在我的情況下重復),
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 計算自動增量編號已被預訂,并且任何其他即將到來的記錄都不會占用它,因為可能同時在 DB 上有多個插入過程并等待知道自動增量編號是否為預訂與否都會降低 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 的問題,而是 MYSQL 的問題.
So it is not a Laravel issue, its a MYSQL one.
我希望它可以幫助其他人.
I hope that it may help others.
謝謝大家...
這篇關于如何禁用 Laravel eloquent Auto Increment?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!