如何禁用Laravel雄辩的Auto Increment? [英] How to disable Laravel eloquent Auto Increment?

查看:122
本文介绍了如何禁用Laravel雄辩的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会像我插入行一样将其计算在内.因此,ID列的排列顺序不是[1、2、3等],而是按[1、4、8、20等].

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计算出自动递增编号已被预订,并且其他任何即将来的记录都不会占用它,因为在同一时间在数据库上可能有多个插入过程,并等待知道自动递增编号是否为是否预订将使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雄辩的Auto Increment?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆