Explicit jsonb type cast in Squeryl - postgresql

I'm using Squeryl 0.9.5-7 and Postgres 9.4 with jsonb datatype and want to insert some data:
case class Log(id: String, meta: String) //meta will contain json
val logs = table[Log]
logs.insert(Log(randomId, "{\"Hi\": \"I'm a json!\"}"))
But got a typecast error that says "Column meta has jsonb type but expression has character varying type. Rewrite expression or convert it's type."
How can I explicitly cast my String field into jsonb so that raw sql-parameter will look like ?::jsonb?
And then, it's interesting how to write json-queries such as #> or ->> with Squeryl?

I found a better way, that doesn't require to create a CAST in the database.
If you're using squeryl 0.9.6, you can add an explicit cast in your schema which tells squeryl to explicitly cast your string into a jsonb.
on(logs)(s => declare(
s.meta is (dbType("jsonb").explicitCast) // enables explicit casting into jsonb
))

With Squeryl 0.9.6 you can register support for your own custom types. Here's an example. For non standard operators, take a look at custom functions

In my experience with postgres 9.4, reading works fine as a Squeryl String but inserting fails with:
ERROR: column "****" is of type json but expression is of type
character varying [error] Hint: You will need to rewrite or cast the
expression. [error] Position: 166 [error] errorCode: 0, sqlState:
42804
So, the solution I found is to create a 'AS ASSIGNMENT' cast in my postgres database and that does the trick:
CREATE CAST(VARCHAR AS JSON)
WITH INOUT
AS ASSIGNMENT

Related

Pyspark's write stringtype argument doesn't deal with null values

I am trying to write a dataset's data into postgres db using jdbc driver.
my_df.write().format('jdbc').mode('append')\
.option('driver', 'org.postgresql.Driver')\
.option('url', 'my_url')\
.option('dbtable', 'my_dbtable')\
.option('user', 'my_user').save()
Apparently pyspark tries to insert all textual types (i.e. uuid) as text by default and throws that error:
Caused by: org.postgresql.util.PSQLException: ERROR: column "id" is of type uuid but expression is of type character varying
Hint: You will need to rewrite or cast the expression.
In order to overcome that issue I had to set a property:
'stringtype':"unspecified"
But that solution does not work on NULL values and throws that error
Caused by: org.postgresql.util.PSQLException: ERROR: column "id" is of type uuid but expression is of type character
Which basically means that it tries to insert the NULL value as character. Separating the dataset to 2 datasets (as #Karuhanga suggested here Pyspark nullable uuid type uuid but expression is of type character varying) is not possible in my case. Did anyone face that issue and found a solution that does not fix a specific column?
Instead of putting a Null value in uuid columns, use as uuid by default like this:
uuid='00000000-0000-0000-0000-000000000000'

Can JOOQ alias a Liquibase JSONB data type for H2/Postgresql

I'm using this H2 feature to create an alias for JSONB in the jdbc string:
spring.datasource.url: jdbc:h2:mem:testdb;INIT=create domain if not exists jsonb as text;MODE=PostgreSQL
But JOOQs' codegen liquibase support (generator pointed at liquibase files) doesn't recognize the JSONB column type.
and I keep getting:
Reason: liquibase.exception.DatabaseException: Unknown data type: "JSONB";
Is there a way to tell the generator to alias this data type to TEXT?
You can try to init your h2 with CREATE TYPE "JSONB" AS json

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.

Graphql mutation for a column of type UUID[]

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

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