apache / apache/grails-core

unique constraint not working

Open
#14,517 4 comments 0 reactions 0 assignees View on GitHub
relates-to: gorm
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

While upgrading from grails 3.1.15 to 3.3.11 we noticed a regression on the unique constraint.
More specifically, a multi-column unique constraint on the same domain class.

The Domain has a reference to itself and the identifier is supposed to be unique between all children of the same parent.

```
class Domain {
String identifier
boolean is_parent
Domain parent

static constraints = {
identifier(nullable: true, blank: true, unique: 'parent')
parent(nullable: true)
}
}
```

We noticed that the query for the unique check is incomplete:
`select this_.id as y0_ from domain this_ where this_.identifier=? limit ?`
the parent is not even considered

This used to work in grails 3.1.15 which generates this query instead:
`select this_.id as id1_0_0_, this_.version as version2_0_0_, this_.identifier as identifi3_0_0_, this_.is_parent as is_paren4_0_0_, this_.parent_id as parent_i5_0_0_ from domain this_ where this_.identifier=? and this_.parent_id=?`

The sample project has an integration test that showcases this.

### Task List

- [X] Steps to reproduce provided
- [ ] Stacktrace (if present) provided
- [X] Example that reproduces the problem uploaded to Github
- [X] Full description of the issue provided (see below)

### Steps to Reproduce

1. Clone the sample repo and run `DomainIntegrationTest`
2. last test fails

### Expected Behaviour

spec should not fail

### Actual Behaviour

spec fails - the unique constraint in the Domain class is not working as expected

### Environment Information

- **Operating System**: MacOS 10.14.6
- **GORM Version:** 6.1.12.RELEASE
- **Grails Version (if using Grails):** 3.3.11
- **JDK Version:** openjdk version "1.8.0_232"

### Example Application

https://github.com/bp-FLN/uniqueConstraint/

Contributor guide

Open the contributing guide

Research direction

Start by cloning the linked example application and running DomainIntegrationTest, especially its last test, to reproduce the failure. Compare the generated unique-check query with the expected query in the issue and trace the relevant GORM/Grails validation path. Done means the integration test passes for a self-referencing domain with a multi-column unique constraint.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.