java - Pass null primary key with jOOQ to have it set by the database -


for rest service, receive object persist in database. want prevent user setting id. instead, database should assign value automatically sequence, postgresql's serial.

if try null out id in jooq generated record, database error out when try store it.

here's relevant ddl.

create table todo (   id serial primary key,   title text,   url text ); 

here's relevant code. full code @ todo-backend.

@post public todo addtodo(todo todo, @context dslcontext db) {     final todorecord todorecord = db.newrecord(todo, todo);      // errors out, violates not-null constraint on db     todorecord.setid(null);      todorecord.store();      todorecord.seturl("http://localhost:8080/" + todorecord.getid());     todorecord.store();      return db.selectfrom(todo)             .where(todo.id.eq(todorecord.getid()))             .fetchoneinto(todo.class); } 

versions

  • dropwizard 0.8.5
  • jooq 3.7.2
  • postgres 9.5
  • java 1.8

some databases allow null values in keys. or perhaps, might have trigger listens null values being set on primary keys.

this why jooq cannot assume intention call set id default. jooq assume want null set in insert or update statements:

todorecord.setid(null); 

instead, write

todorecord.changed(todo.id, false); 

see record.changed(field, boolean)


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -