Search code examples
mysqlmariadbjooq

How to add auto increment primary column with jOOQ


In MySQL/MariaDB I tried to do something like:

ctx.alterTable("my_table")
   .addColumn("id", SQLDataType.BIGINT.identity(true))
   .execute();
ctx.alterTable("my_table")
   .add(DSL.primaryKey("id"))
   .execute();

But the first step fails because auto increment column must be a key. Is there a clean way to do this that I missed, or should I just do something like .getSql() + " primary key"?


Solution

  • You can add multiple things with the ALTER TABLE statement, if your RDBMS supports that:

    ctx.alterTable("my_table")
       .add(
            field("id", SQLDataType.BIGINT.identity(true)),
            constraint("pk").primaryKey("id"))
       .execute();