Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Postgres: array of NULL values

I have a table defined as:

CREATE TABLE IF NOT EXISTS test (
    id INT NOT NULL,
    depth_mm REAL[]
);

If I insert something with both numbers and null values in the depth_mm array, there is no problem:

INSERT INTO test (id, depth_mm) VALUES (2, ARRAY[1,NULL,NULL]);

But if I insert an array of values, it complains:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

INSERT INTO test (id, depth_mm) VALUES (2, ARRAY[NULL,NULL,NULL]);

ERROR:  column "depth_mm" is of type real[] but expression is of type text[]
LINE 1: INSERT INTO test (id, depth_mm) VALUES (2, ARRAY[NULL,NULL,NU...
                                                   ^
HINT:  You will need to rewrite or cast the expression.

How do I get Postgres to understand that I want to insert an array of null values?

>Solution :

The engine can’t infer the type of the array since all you have is NULLs there, and NULL can be of any type. So you’d somehow need to tell it it’s a real NULL by casting it to real:

INSERT INTO test (id, depth_mm) VALUES (2, ARRAY[NULL::real,NULL,NULL]);

Or you can cast the entire array:

INSERT INTO test (id, depth_mm) VALUES (2, ARRAY[NULL,NULL,NULL]::real[]);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading