3

Given:

CREATE TABLE foo (id BIGINT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id));

I'd like to invoke: INSERT INTO foo VALUES() and get back the generated key. I tried following the advice found at jOOQ insert query with returning generated keys but when I invoke:

RoomsRecord record = db.insertInto(foo, Collections<Field<?>>emptyList()).returning(foo.ID).fetchOne();

JOOQ returns null instead of the generated key. Is this a bug?

Community
  • 1
  • 1
Gili
  • 86,244
  • 97
  • 390
  • 689

1 Answers1

3

This seems to be a bug in FieldMapsForInsert.isExecutable(). It flags the above query as non-executable but it is legal under MySQL. I filed https://github.com/jOOQ/jOOQ/issues/20

You can insert NULL as a workaround. Furthermore, you must configure JOOQ to generate table "relations", otherwise returning() won't see the existence of primary keys and return null.

<configuration>
  <generator>
    <generate>
      <relations>true</relations>
    </generate>
  </generator>
</configuration>
Community
  • 1
  • 1
Gili
  • 86,244
  • 97
  • 390
  • 689