如何禁用 Laravel eloquent Auto Increment?

时间:2023-03-03
本文介绍了如何禁用 Laravel eloquent Auto Increment?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我需要一种有效的方法来禁止 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:合并和排序两个 Eloquent 集合? 下一篇:Laravel 使用 Eloquent 的多对多关系

相关文章