3

Using Jooq 3.13.5, Postgresql 10.

The expectation is that the insert query will insert the row into the database table if it doesn't already exist and return the id column regardless.

Table:

CREATE descriptor_table ( id serial primary key, name text unique );

Working SQL query:

INSERT INTO descriptor_table (name)
  VALUES('a name')
ON CONFLICT("name")
  DO UPDATE SET name=EXCLUDED.name
RETURNING id;

Java code:

DSLContext dsl = jooqProvider.getDSLContext();
dsl.transaction(configuration -> {
  MyRecord                                                             
      idValue =                                                              
      DSL.using(configuration)                                                 
         .insertInto(DESCRIPTOR_TABLE)                                               
         .set(DESCRIPTOR_TABLE.NAME, "a name")                            
         .onConflict(Keys.DESCRIPTOR_TABLE_NAME_KEY.getFieldsArray())                
         .doUpdate()                                                           
         .set(DESCRIPTOR_TABLE.NAME, "EXCLUDED.name")                                
         .where(DESCRIPTOR_TABLE.NAME.eq("a name")))                       
         .returning(DESCRIPTOR_TABLE.ID)                                             
         .fetchOne();
}

The resulting MyRecord value is null.

1 Answer 1

3

Your string "EXCLUDED.name" is just a string bind variable, not an expression. It's no different from setting the NAME column to e.g. "Mike" or like your other bind variable "a name".

If you want a plain SQL expression, use DSL.field(String, DataType) or a similar overload:

.set(DESCRIPTOR_TABLE.NAME, field("EXCLUDED.name", DESCRIPTOR_TABLE.NAME.getDataType()))

Note that your jOOQ query has a where() clause, unlike your SQL query...

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.