DB Insert Timing - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: DB Insert Timing (/showthread.php?tid=86156) Pages:
1
2
|
DB Insert Timing - SoccerGuy3 - 01-16-2023 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! RE: DB Insert Timing - kenjis - 01-16-2023 (01-16-2023, 03:59 PM)SoccerGuy3 Wrote: Is CI holding the connection open Yes in a request unless you close the connection. (01-16-2023, 03:59 PM)SoccerGuy3 Wrote: and then MariaDB uses the time the connection was opened for the timestamp or something? I guess not. Can you show minimum sample code? RE: DB Insert Timing - SoccerGuy3 - 01-16-2023 (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), RE: DB Insert Timing - kenjis - 01-16-2023 How do you set the created_at value? Using Model $useTimestamps? https://codeigniter4.github.io/CodeIgniter4/models/model.html#usetimestamps RE: DB Insert Timing - SoccerGuy3 - 01-16-2023 (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/ ). RE: DB Insert Timing - kenjis - 01-16-2023 Check your real SQL statements with Debug Toolbar or DB Server log. RE: DB Insert Timing - SoccerGuy3 - 01-17-2023 (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 RE: DB Insert Timing - kenjis - 01-17-2023 The $useTimestamps is false by default. https://codeigniter4.github.io/CodeIgniter4/models/model.html#usetimestamps If it is false, CI Model does not set the timestamps. Check your model code. RE: DB Insert Timing - InsiteFX - 01-18-2023 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 ( RE: DB Insert Timing - SoccerGuy3 - 01-18-2023 (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? |