Query filter for array of composite type #2875
-
Hi! I have a table that has an array of a composite type and am trying to figure out how to create an API query that will search the array. I've tried iterating on a lot of different syntaxes and haven't found the solution yet. The use case is basically this: #2743, but instead of using json or jsonb I'm instead using a composite type. Maybe the composite type won't work and I need to switch(?), but I was hoping to find out for sure from the experts before making a switch on data types. Here's an example that I hope illustrates the problem. SQL: CREATE TYPE alias AS (source text, alias text);
CREATE TABLE table_person (
id varchar(36) PRIMARY KEY,
aliases alias []
);
CREATE VIEW "person" AS
SELECT
t."id" AS "id",
t."aliases" AS "aliases"
FROM
"table_person" t; If I try something like this:
I get this error return: {
"hint": null,
"details": "\"[\" must introduce explicitly-specified array dimensions.",
"code": "22P02",
"message": "malformed array literal: \"[{\"alias\":\"some alias to find\"}]\""
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Hello! I'm not sure if this can be done directly with custom types. The But since you're using a VIEW to query the data, it should be easier to just convert your custom type to CREATE VIEW "person" AS
SELECT
t."id" AS "id",
t."aliases" AS "aliases",
to_jsonb(t."aliases") AS "aliases_jsonb" -- convert custom type to jsonb
FROM
"table_person" t; Then your query should work with that new column:
You could also convert "aliases" directly in the view if you prefer (though some things may change, like the order of the custom type columns). |
Beta Was this translation helpful? Give feedback.
-
Thanks! If I wanted to find out for certain whether it will work without using jsonb do you have any suggestions for how I would go about that? |
Beta Was this translation helpful? Give feedback.
While the computed relationship way would work as well, it's rather complicated. However, using a computed column, this can be done easily:
and then