1
votes

I am using JOOQ(newbie in JOOQ) to create the database at runtime using Rest API in my spring boot project. In one of the case I need to create a table with a composite primary key which can be combination of multiple columns. I am using the below piece of code to create constraint -

ArrayList<Constraint> constraints = new ArrayList<>();
constraints.add(constraint(name("pk_" + tableName))
.primaryKey(field("column1"), field("column2")));

I already have List<Field<?>> which will be working as the composite primary key. How can I make this dynamic as the primary key constraint can support n number of columns? Is there any way I can directly provide the field list in the .primarykey() API?

1

1 Answers

1
votes

The usual overloads for primaryKey(Collection<? extends Field<?>>) that one may expect in such a case are missing from the constraint API. I've create a feature request for jOOQ 3.15: https://github.com/jOOQ/jOOQ/issues/11816

You can just use ConstraintTypeStep.primaryKey(Field<?>...) using standard JDK Collection.toArray() methods:

List<Field<?>> list = ...

// Pre Java 11
constraints.add(constraint(...).primaryKey(list.toArray(new Field<?>[0])));

// Using new JDK 11 API
constraints.add(constraint(...).primaryKey(list.toArray(Field<?>[]::new)));