grails-data-mapping icon indicating copy to clipboard operation
grails-data-mapping copied to clipboard

Grails 5 - UniqueConstraint ignores property in group if value is null - can't use it on nullable columns - worked in Grails 2.5

Open boardbloke opened this issue 3 years ago • 1 comments

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.

boardbloke avatar Dec 17 '21 16:12 boardbloke

Looks like a duplicate of https://github.com/grails/grails-data-mapping/issues/1194

boardbloke avatar Dec 19 '21 23:12 boardbloke