Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

INSERT multidimensional array to Postgres

Is there any hack when inserting multidimensional array to postgres? I would like to insert smth like

INSERT INTO Timesheet (day, startTime, endTime, idSchedule)
VALUES(unnest(array['TUE', 'FRI']),
       unnest(array[['10:00', '02:00'], ['10:00']]),
       unnest(array[['14:00', '23:00'], ['14:00']]), 1);

Or in detail

INSERT INTO Timesheet (day, startTime, endTime, idSchedule)
VALUES('TUE', '10:00', '14:00', 1),
      ('TUE', '02:00', '23:00', 1),
      ('FRI', '10:00', '14:00', 1);

Query fails with

ERROR: multidimensional arrays must have array expressions with matching dimensions

Maybe there are any solutions?

like image 903
Jane Avatar asked Jul 26 '26 16:07

Jane


1 Answers

You're probably looking for

INSERT INTO Timesheet (day, startTime, endTime, idSchedule)
VALUES(unnest(array['TUE', 'TUE', 'FRI']),
       unnest(array['10:00', '02:00', '10:00']),
       unnest(array['14:00', '23:00', '14:00']), 1);
like image 194
Bergi Avatar answered Jul 29 '26 07:07

Bergi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!