Open
Description
Consider the following schema:
CREATE TABLE my_table (my_array text[]);
If you run:
sql.unsafe(`insert into my_table (my_array) values ($1)`, [['item']]);
...it works as expected.
Now instead of using a built-in type, change the array to be a custom array type:
CREATE DOMAIN my_custom_array_type AS text[];
CREATE TABLE my_table (my_array my_custom_array_type);
Running the same code:
sql.unsafe(`insert into my_table (my_array) values ($1)`, [['item']]);
...now fails with the following error:
Uncaught PostgresError: malformed array literal: "item"
There appears to be an issue with serializing arrays, but only for custom array types.
Why would you want to use such custom types? One possible reason is if you want to constrain the length of the array, e.g.
CREATE DOMAIN my_custom_array_type AS text[]
CONSTRAINT my_custom_array_type_check CHECK ((array_length(VALUE, 1) <= 3)) // At most 3 elements
It appears to have something to do with how we're defining the serializers for these custom types:
Lines 725 to 736 in 364c3eb
Metadata
Metadata
Assignees
Labels
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
porsager commentedon Apr 27, 2023
Just merged a PR that should fix this #578
Can you check how that works?
Bas950 commentedon Apr 27, 2023
They probably still have to make their own parser/serialiser for it.
sds commentedon Apr 27, 2023
I still receive the same error with that change.
I might be misunderstanding, but this feels like an example where a custom parser/serializer shouldn't be necessary. In the example,
my_custom_array_type
is effectively an alias oftext[]
, so it should already work with the built-in serializers.Perhaps there's something about the query in
fetchArrayTypes
that isn't making that association work?sds commentedon Apr 27, 2023
Here's a test that reproduces the failure:
Bas950 commentedon May 2, 2023
I guess it would be possible, but not sure how it should be implemented.
You can fetch the base type this way (there is probs an easier query I just edited the current one):
@porsager any ideas?
marcbachmann commentedon May 7, 2023
This query might help in case somebody wants to write a custom type serializer/parser: