1

postgres supports multi dimensional arrays, however the length is restricted.

CREATE TABLE test (
    field1 text[][]
);

INSERT INTO test VALUES ('{{null, null}, {foo, null}, {foo, bar}}');

this works fine, but if I do

INSERT INTO test VALUES ('{{}, {foo}, {foo, bar}}');

this gives an error 'malformed array literal: "{{}, {foo}, {foo, bar}}"'. Can I do something like the above but without errors?

3

1 Answer 1

1

You could use a domain:

CREATE DOMAIN x AS text[];

CREATE TABLE test (field1 x[]);

INSERT INTO test VALUES (ARRAY[
                            ARRAY[]::x,
                            ARRAY['foo']::x,
                            ARRAY['foo', 'bar']::x
                         ]);

TABLE test;

           field1           
════════════════════════════
 {"{}","{foo}","{foo,bar}"}
(1 row)
Sign up to request clarification or add additional context in comments.

2 Comments

Is there a way to insert by literal constant? Because I'm using Hasura and it only works with array literal
Sure, use the exact format of the final query result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.