Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

How to Add Schema Name to Foreign Key Methods for Non-`main` Schemas in jOOQ's `MatcherStrategy`

45 views
Skip to first unread message

k s

unread,
Oct 28, 2024, 2:52:17 AM10/28/24
to jOOQ User Group
Hello,

I'm encountering a warning due to ambiguous key names generated by jOOQ during code generation. The issue arises from foreign keys across different schemas having identical method names. Here's an example:

- `main.users.service_id` references `main.service.service_id` as `USERS__USERS_SERVICE_ID_FKEY`
- `schema_a.users.service_id` also references `main.service.service_id`, leading to the same generated method name: `USERS__USERS_SERVICE_ID_FKEY`

This duplication triggers the following warning during `generateJooq`:

> Ambiguous key name: The database object `schema_a.users_service_id_fkey` generates an inbound key method name `users`, which conflicts with the previously generated outbound key method name. Use a custom generator strategy to disambiguate the types. More information here:

To resolve this, I’d like to use `codegen-matcherstrategy` to automatically include the schema name for foreign key method names in all schemas except `main`. I reviewed the documentation at [https://www.jooq.org/doc/latest/manual/code-generation/codegen-matcherstrategy/](https://www.jooq.org/doc/latest/manual/code-generation/codegen-matcherstrategy/) but need guidance on implementing this.

Here’s my current configuration setup:

```groovy
strategy {
    name = 'org.jooq.codegen.DefaultGeneratorStrategy'
    matchers {
        foreignKeys {
            foreignKey {
                // HELP WANTED ...
            }
        }
    }
}
```

Any help in customizing this configuration so that non-`main` schemas append the schema name to foreign key method names would be greatly appreciated.

Thank you!

Lukas Eder

unread,
Oct 28, 2024, 3:11:22 AM10/28/24
to jooq...@googlegroups.com
Thanks for your message. I'd say, this is a bug. You shouldn't be getting this warning in that case, given that the keys are generated into different Keys classes from different packages:

As documented here:

Just add the expression (regular expression matching keys in your schema) and the matcher rule for the keyIdentifier:

e.g.

foreignKey {
  expression = ".."
  keyIdentifier {
    transform = "UPPER"
    expression = "PREFIX_$0"
  }
}

The only way to automatically include the schema name is to repeat this configuration for each schema, or to create a programmatic configuration instead:


--
You received this message because you are subscribed to the Google Groups "jOOQ User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/jooq-user/9ce75264-3b92-47aa-b227-cdda768b7a2an%40googlegroups.com.

Lukas Eder

unread,
Oct 28, 2024, 3:12:56 AM10/28/24
to jooq...@googlegroups.com
My bad, this was for the method names, not the identifier. So, instead of keyIdentifier, specify pathMethodName, etc.

k s

unread,
Oct 29, 2024, 1:17:20 AM10/29/24
to jOOQ User Group

  Thank you for the response. I understand that this is a bug. For now, I'll try the suggested method and see how it goes.  
2024年10月28日月曜日 16:11:22 UTC+9 lukas...@gmail.com:
Reply all
Reply to author
Forward
0 new messages