DB Insert Timing |
Ran into something strange today. Running CI4.2.11 with PHP 8.025
I have a process setup that takes some input, does an insert into the database, opens a dialog box which is a confirmation, takes that click, then 2 searches are run and then another insert is run. All works great, except according to the timestamp in MariaDB, both inserts take place at the exact same time - down to 6 decimal places! I tried inserting a "usleep(150000)" before the second insert, but it didn't make any difference (actually not sure it is even being recognized/executed). Time of first insert: 2023-01-16 14:48:41.920000 Time of second insert: 2023-01-16 14:48:41.920000 Because of this, attempting to find the most recent record is failing (well, actually it is returning BOTH records). Is CI holding the connection open and then MariaDB uses the time the connection was opened for the timestamp or something? Help! ![]()
(01-16-2023, 04:49 PM)kenjis Wrote: Can you show minimum sample code? Sure. Here it is (I cut out a bunch of stuff that isn't relative - the code works, it is just the time stamp I am questioning): PHP Code: $data = array( In case it helps, the field definition: Code: `created_at` datetime(6) NOT NULL DEFAULT current_timestamp(3),
How do you set the created_at value?
Using Model $useTimestamps? https://codeigniter4.github.io/CodeIgnit...timestamps
(01-16-2023, 05:20 PM)kenjis Wrote: How do you set the created_at value? No, I let MariaDB set it on record creation. The field is a DateTime column with a default value of "Current_TimeStamp" ( https://mariadb.com/kb/en/datetime/ ).
Check your real SQL statements with Debug Toolbar or DB Server log.
(01-16-2023, 09:03 PM)kenjis Wrote: Check your real SQL statements with Debug Toolbar or DB Server log. Code: INSERT INTO `auction_lots_versions` (`parent_id`, `created_at`, `createdby_id`) Ok that tells me what is going on. CI is overriding and putting the 'created_at' in there, rather than allowing MariaDB to do it as I thought it was doing. I remember reading something about that in the docs. Off to find it! Thanks Kenjis you are always so helpful!! I very much appreciate it! So looking at my Model, I see I included the "created_at" field in the allowed fields. Simply removing this solves the issue and allows MariaDB to set the timestamps. PHP Code: namespace App\Models; Quote:Since I don't specify the field, shouldn't it NOT set the timestamp?Quote:$createdField
The $useTimestamps is false by default.
https://codeigniter4.github.io/CodeIgnit...timestamps If it is false, CI Model does not set the timestamps. Check your model code.
If a TIMESTAMP or DATETIME column definition includes an explicit fractional seconds precision value anywhere,
the same value must be used throughout the column definition. This is permitted: Code: CREATE TABLE t1 ( This is not permitted: Code: CREATE TABLE t1 ( What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
(01-17-2023, 06:37 PM)kenjis Wrote: The $useTimestamps is false by default. My model code is in the post above yours. I didn't set the timestamp one way or the other. Maybe I need to explicitly set it to false. Could it be set in a config file or something? |
Welcome Guest, Not a member yet? Register Sign In |