Grails 5 - UniqueConstraint ignores property in group if value is null - can't use it on nullable columns - worked in Grails 2.5
- Dominant language
- Groovy
- Stars
- 2.9k
- Forks
- 975
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 92
Description
I have a class that have fields that participate in a nullable constraint thus:
class ContentItem implements Serializable {
String code
String channel
String content
static constraints = {
code(nullable: false, unique: ['channel'])
channel(nullable: true, unique: ['code'])
content(nullable: false)
}
The idea is that I can have content, identified by a code, specified for a unique combination of code and channel. Because the channel property is nullable I can specify a row that contains a code and "null" for the channel to provide generic content when a channel does not have a specific content match in the database.
However, In Grails5 I cannot save two rows thus:
code | channel | content
'aCode' | 'aChannel' | 'someContent'
'aCode' | null | 'defaultContent'
The second row fails the unique test.
Looking at the code in UniqueConstraint.groovy it looks like the detachedCriteria that is build to determine whether a record already exists only adds an equality test for a property in the array specified in the unique constraint in the constraints closure if the value of that property is not null. But that means it cannot cope with nullable DB columns.
Contributor guide
Research direction
Start with UniqueConstraint.groovy and inspect how the detachedCriteria is built for grouped unique properties when one value is null. Reproduce the ContentItem example with nullable channel; done means the generic and channel-specific rows both pass the uniqueness check without regressing non-null grouped constraints.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- groovy
- Domain
- backend, databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100