Graphql mutation for a column of type UUID[] - postgresql

I'm using a postgraphql autogenerated mutation to create a row with graphql in a in a postgres table that has a column of datatype UUID[].
However, there doesn't seem to be a way to save this UUID[] data with graphql? Is this a datatype that graphql doesn't account for or am I wrongly forming the array?
When I go to create the row in graphiql:
mutation {
createJob(
input: {
user_ids: ["5b7323ac-e235-4edb-bbf9-97495d9a42a1"],
instructions: "Job Instructions",
publishedDate: "2017-06-07"}
}
)
}
I get the following error:
"message": "column \"user_ids\" is of type uuid[] but expression is of type text[]"
Is a UUID technically not stored like text? I've tried different ways of forming the the UUID array but nothing seems to work

You can probably have a workaround to this problem by creating an implicit cast:
CREATE CAST (text[] AS uuid[])
WITH INOUT
AS IMPLICIT ;
PostgreSQL will automatically do the conversion from text[] to uuid[]; which just seems that is the needed step.
NOTE: The above code must be executed from "the owner of type uuid[] or text[]", which usually means: postgres or the database owner.
To check that it works, you may just do:
CREATE TABLE t
(
ids uuid[]
) ;
INSERT INTO t
(ids)
VALUES
(ARRAY['5b7323ac-e235-4edb-bbf9-97495d9a42a1','5b7323ac-e235-4edb-bbf9-97495d9a42a2']),
(ARRAY['6b7323ac-e235-4edb-bbf9-97495d9a42a1']) ;
... and then, test it with GraphQL

This was a bug in PostGraphQL; it's been fixed in version 4 (which has not been released yet, but you can install via npm install postgraphql#next)
More information: https://github.com/postgraphql/postgraphql/issues/516

Related

How do you manually insert a uuid type field in Postgres SQL DB?

I have a Postgres table with a field called user_uuid with type uuid. I also have a valid UUID value that I want to manually insert in that row, but I can't seem to find a way to manually create this.
This is an example of the statement I'm trying to execute:
insert into my_table (account_number, type, user_uuid) values ('1252', 'residential', 'dOfa6513-aOfd-4e78-9941-724b22804e9f');
I've tried appending ::UUID which I read somewhere might work, and to enclose the UUID text value inside curly brackets, instead of single quotes. None of that has worked, and the docs are not helpful either. The error I get is the following:
invalid input syntax for type uuid: 'dOfa6513-aOfd-4e78-9941-724b22804e9f'
The UUID you're trying to insert is not a valid UUID.
You can check the validity here https://www.freecodeformat.com/validate-uuid-guid.php
This is one example of a valid UUID: a8adfa00-6680-49b3-bf94-caa8c3f1d823,
can try pass this into your insert query and check if ok.
There are 2 occurrences of the letter O in your uuid.
It should have been the digit 0 instead (zero) to make it a proper hexadecimal string: d0fa6513-a0fd-4e78-9941-724b22804e9f

how to build using expression for data conversion when alter column type

I have a column Status with type smallint, which I want to convert it to uuid. The new value should be generated like this:
If the old value equals to 0 then take uuid_nil() as new value
Otherwise, take a random value from uuid_generate_v1()
The convert logic described above could be changed to a simple one if it's not possible to achieve.
I checked the pg docs here and I guess I should use a USING clause to do that but I am not sure where to start with this expression.
ALTER TABLE myschema."MyTable"
ALTER COLUMN "MyColumn" TYPE uuid
USING ?
Could anyone show an example or some instruction docs?
With no uuid-ossp module dependency:
ALTER TABLE myschema."MyTable"
ALTER COLUMN "MyColumn" TYPE uuid
using (
case
when "MyColumn" = 0 then '00000000-0000-0000-0000-000000000000'::uuid
else gen_random_uuid()
end
);
This will generate UUID v4 and not v1.
It looks like people find answer to their question right after they post it in StackOverflow.
A CASE ... THEN ... ELSE ... expression works for me.
ALTER TABLE myschema."MyTable"
ALTER COLUMN "MyColumn" TYPE uuid
USING CASE WHEN "MyColumn"=0 THEN uuid_nil() ELSE uuid_generate_v1() END
Although uuid_generate_v1() always generateds the same value but it can be fixed out of box later.

Slick 3 - how to get correct (database) schema when inserting with plain SQL

I'm trying to get basic plain SQL example working in Slick 3, on Postgres but with custom DB schema, say local instead of default public one. I have hard time inserting the row as executing the following
sqlu"INSERT INTO schedule(user_id, product_code, run_at) VALUES ($userId, $code, $nextRun)"
says
org.postgresql.util.PSQLException: ERROR: relation "schedule" does not exist
The table is in place because when I prefix schedule with local. in the insert statement it works as expected. How can I get correct schema provided to this query?
I'm using it as part of akka-projection handler and all the projection internals like maintaining offsets work as expected on local schema.
I cannot simply put schema as a variable as it errors while resolving parameters:
sqlu"INSERT INTO ${schema}.schedule(user_id, product_code, run_at) VALUES ($userId, $code, $nextRun)"
You can insert schema name using #${value}:
sqlu"INSERT INTO #${schema}.table ..."

JSONB PostgreSQL with jOOQ 3.10

How can I write String variable to PostgreSQL JSONB column without generated classes using jOOQ 3.10?
dsl.insertInto(table, Arrays.asList(
DSL.field("configuration")
))
.values(
data.getConfiguration()
).execute();
I have a json string into data.getConfiguration(), but I get exception
org.postgresql.util.PSQLException: ERROR: column "configuration" is of type jsonb but expression is of type character varying
The answer is the same as for your previous question. Write a data type binding (or better: upgrade your jOOQ version!).
DSL.field(name("jsonb_column"),
SQLDataType.VARCHAR.asConvertedDataType(new MyJSONBBinding()));
The manual link on the previous answer I've given shows how to do exactly that.

Postgres CREATE TABLE AS SELECT with unknown column type

I want to create a table with the following query:
create table something as
select
other_id,
other_title,
'0.0.0.0' as IP,
1 as used
from
other_table
But Postgres complains that:
column "ip" has type "unknown"
How can I specify it to be of type char?
You need to explicitly cast your column, like this:
'0.0.0.0'::INET as IP,
At least since Postgres 9.4, this does not raise an EXCEPTION, just a WARNING. The table is created anyway, with a column of data type unknown if no type is given for the string literal:
dbfiddle for pg 9.4 here
Postgres 10 introduces more useful default behavior. String literals are cast to the default data type text unless cast explicitly. So you don't get columns of type unknown any more:
dbfiddle here
Introduced with this commit (with detailed explanation).
And the type unknown has been labeled a pseudo-type now. Details in this commit.
You should specify the type , use '0.0.0.0'::text AS IP