codeconsole opened a new issue, #15680:
URL: https://github.com/apache/grails-core/issues/15680
## Summary
When a domain class field has both `unique: true` in `static constraints`
and `index: true, indexAttributes: [unique: true]` in `static mapping`, the
MongoDB index is silently not created. Removing the constraint allows the index
to be created normally.
## Steps to Reproduce
1. Create a domain class with the following:
```groovy
class Category {
String name
static constraints = {
name unique: true
}
static mapWith = 'mongo'
static mapping = {
name index: true, indexAttributes: [unique: true]
}
}
```
2. Start the application and insert data via BootStrap
3. Query MongoDB for the collection's indexes:
```javascript
db.category.getIndexes()
```
**Result:** Only `_id_` index exists. The `name_1` unique index is missing.
4. Remove the constraint:
```groovy
static constraints = {
name() // removed unique: true
}
```
5. Restart and check indexes again.
**Result:** `name_1` unique index is now created correctly.
## Expected Behavior
The `index: true, indexAttributes: [unique: true]` declaration in the
mapping block should create the MongoDB index regardless of whether the
constraints block also declares `unique: true`. The constraint and the mapping
serve different purposes — constraints validate at the GORM layer, while
`indexAttributes` creates the database-level index.
## Actual Behavior
The presence of `unique: true` in the constraints block silently prevents
`initializeIndices()` from creating the MongoDB index declared in the mapping
block. No error or warning is logged.
## Additional Context
- Fields with plain `index: true` (no `indexAttributes`) are created
correctly even with constraint declarations
- Fields with `index: true, indexAttributes: [unique: true]` but WITHOUT a
constraint `unique: true` are created correctly
- The `compoundIndex([name: 1, indexAttributes: [unique: true]])` syntax is
not affected — it always creates the index regardless of constraints
- Other domain classes (Country, Type) with the same `index: true,
indexAttributes: [unique: true]` mapping but WITHOUT `unique: true` in
constraints create their indexes correctly
## Environment
- Grails: 7.2.0-SNAPSHOT
- GORM MongoDB: 7.2.0-SNAPSHOT (`grails-data-mongodb-core`)
- MongoDB: embedded (de.flapdoodle)
- Java: 25
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]