Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
boardbloke opened this issue Dec 17, 2021 · 1 comment

Comments

@boardbloke
Copy link

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
Copy link
Author

Looks like a duplicate of #1194

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant