Using db insert in CI3 auto increment off in table? |
05-17-2023, 08:13 AM
(This post was last modified: 05-17-2023, 08:14 AM by xanabobana. Edit Reason: updated error text as code for easier readability )
Hi- I have a strange behavior happening and I'm wondering if anyone can help! I have a table with Users, that has an auto_increment field for the ID. Yesterday, the account approval process started failing and I see in the CI logs:
Code: ERROR - 2023-05-17 10:24:52 --> Query error: Duplicate entry '10084' for key 'PRIMARY' - Invalid query: INSERT INTO `tblUser` (`active`, `first_name`, `last_name`, `company`, `phone`, `email`, `username`, `password`, `ip_address`, `created_on`) VALUES (0, 'Xana', 'Wolf', 'uvm', '000-000-0000', '[email protected]', 'Xana Wolf', '$argon2i$v=19$m=4096,t=3,p=1$beO82X4wsRUxieorH5iRjg$C9HPtN9TYYmLLw+/VPJGzgh6ieZidOHwXwbcYtkj/OE', '132.198.100.190', 1684333492) The code generating the error above is this: Code: $data = [ The insert statement looks correct to me, so I'm confused why the auto_increment value keeps getting out of whack? I can add something in the code to check the max ID and make sure the auto_increment value is set correctly, I just would like to figure out why this is happening! I did find a thread about the mysql NO_AUTO_VALUE_ON_ZERO setting, but I checked and it wasn't set.
This is more a mySQL problem than a CI problem. Depending on how you have reset the autoincrement for the id field, it might be that there are some gaps in the id, causing mySQL to think that it can autoincrement starting at a lower value than the actual highest id.
Check the autoincrement value using: Code: SHOW TABLE STATUS LIKE 'table_name' Set the autoincrement one higher than the absolute highest value in your table: Code: ALTER TABLE table_name AUTO_INCREMENT=10085; Now, mySQL will autoincrement as expected.
I did reset the auto increment. The first insert is fine, but then the second fails because the auto increment isn't incrementing...
|
Welcome Guest, Not a member yet? Register Sign In |