constraintsjooqdatabase-agnostic

jOOQ: Database-agnostic way of disabling constraints


I'd like to know if there's a way to disable database constraints in jOOQ for different databases in an agnostic way. For example, disabling foreign key constraints in MySQL and PostgreSQL differs quite a lot:

MySQL

SET FOREIGN_KEY_CHECKS=0;

PostgreSQL

Not actually too sure, but will probably involve disabling triggers.


Solution

  • This kind of feature is extremely vendor specific - it would be hard to standardise on a feature like this. In jOOQ's DDL interpreter, however, this is supported starting from jOOQ 3.13: https://github.com/jOOQ/jOOQ/issues/8105

    But since you're only looking for support in two databases, you could roll your own:

    void disableForeignKeys(Configuration configuration) {
        switch (configuration.family()) {
            case MYSQL:
                DSL.using(configuration).execute("set foreign_key_checks=0");
                break;
            case POSTGRES:
                ...
        }
    }