apache / apache/grails-core

multi-column unique constraint is ignored

Open
#14,661 16 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

I've defined 2 domain classes that are mapped using the "no collections" approach described [here](https://mrpaulwoods.wordpress.com/2011/02/07/implementing-burt-beckwiths-gorm-performance-no-collections/)

``` groovy
class IncomeScenario {

String title

Collection getItems() {
IncomeDelta.findAllByIncomeScenario(this)
}

def beforeDelete() {
withNewSession {
executeUpdate('delete IncomeDelta where incomeScenario = ?', [this])
}
}
}

class IncomeDelta {

IncomeScenario incomeScenario
String name

static constraints = {
// can't have multiple income scenario items with the same name
name unique: 'incomeScenario'
}
}
```

However, it seems this constraint is ignored because I'm not prevented from saving an `IncomeScenario` that has multiple items with the same name, e.g.

``` groovy
def scenario = new IncomeScenario(title: 'foo').save()
new IncomeDelta(name: 'name', incomeScenario: scenario).save()

// this one should fail, but it doesn't
new IncomeDelta(name: 'name', incomeScenario: scenario).save(failOnError: true)
```

Contributor guide

Open the contributing guide

Research direction

Start by running the issue's two IncomeScenario/IncomeDelta save calls and confirm that the duplicate name is accepted despite the `name unique: 'incomeScenario'` constraint. Trace the constraint and persistence behavior for this no-collections mapping; done means the second save is rejected as the example expects.

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
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.