objection.js icon indicating copy to clipboard operation
objection.js copied to clipboard

afterInsert result fails to retrieve autogenerated datetime

Open anthonyma94 opened this issue 3 years ago • 0 comments

I'm using mariadb v10.4.25 with mysql2 drivers.

I have a class with an afterInsert hook as so:

CREATE TABLE `production_job_status` (
  `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `job` varchar(30) NOT NULL,
  `assigned_to` varchar(255) DEFAULT NULL,
  `progress` int(2) DEFAULT 1,
  `order` int(3) DEFAULT NULL,
  `created_at` datetime(6) DEFAULT current_timestamp(6),
  `assigned_at` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
export default class ProductionJobStatus {
    id: number;
    job: string;
    assignedTo?: string;
    progress?: number;
    order?: number;
    assignedAt?: Dayjs;
    createdAt: Dayjs;
    static async afterInsert(args) {
        console.log(args.result[0].createdAt); // undefined
    }
}

args.result[0] will retrieve the newly inserted record, including the id, which is auto incremented. However, createdAt is nowhere to be found in the object. How can this be so?

anthonyma94 avatar Oct 13 '22 17:10 anthonyma94