gorm-mongodb icon indicating copy to clipboard operation
gorm-mongodb copied to clipboard

Unique constraint violation when saving object with one to one relationship in Grails 4.0.0.RC2 and GORM7

Open PrinceAdeleke opened this issue 5 years ago • 3 comments

I've created a small grails application where a Book has an Author. I create an instance of book and author and saved the objects to the database. In the Book class I have an event beforeValidate that tags the book by concatenating the title and author name. When saving the object an exception is thrown grails.validation.ValidationException: Validation error occurred during call to save().

Version:

Grails

4.0.0.RC2

Mongodb

7.0.0 (org.grails.plugins:mongodb:7.0.0 in build.gradle)

Expected Behaviour

The book should update without any constraint violations.

Actual Behaviour

When saving the book an error is thrown. The error states that the author name has been rejected and must be unique.

arguments [name,class library.Author,Charles Perrault]; default message [Property [{0}] of class [{1}] with value [{2}] must be unique]. See stacktrace below.

Stacktrace:

- Field error in object 'library.Book' on field 'author.name': rejected value [Charles Perrault]; codes [library.Author.name.unique.error.library.Book.author.name,library.Author.name.unique.error.author.name,library.Author.name.unique.error.name,library.Author.name.unique.error.java.lang.String,library.Author.name.unique.error,author.name.unique.error.library.Book.author.name,author.name.unique.error.author.name,author.name.unique.error.name,author.name.unique.error.java.lang.String,author.name.unique.error,library.Author.name.unique.library.Book.author.name,library.Author.name.unique.author.name,library.Author.name.unique.name,library.Author.name.unique.java.lang.String,library.Author.name.unique,author.name.unique.library.Book.author.name,author.name.unique.author.name,author.name.unique.name,author.name.unique.java.lang.String,author.name.unique,unique.library.Book.author.name,unique.author.name,unique.name,unique.java.lang.String,unique]; arguments [name,class library.Author,Charles Perrault]; default message [Property [{0}] of class [{1}] with value [{2}] must be unique]

Example Application

Bootstrap.groovy

package bookstore

import library.Author
import library.Book

class BootStrap {

    def init = { servletContext ->
        Author.findAll().each { it.delete(flush: true, failOnError: true) }
        Book.findAll().each { it.delete(flush: true, failOnError: true) }

        Author author = new Author(name: "Charles Perrault").save(flush: true, failOnError: true)
        new Book(title: 'Little Red Riding Hood', author: author).save(flush: true, failOnError: true)

        Book.withNewSession {
            Book book = Book.findByTitle('Little Red Riding Hood')
            book.save(failOnError: true)
        }
    }
}

Author.groovy

package library

import org.bson.types.ObjectId

class Author {

    ObjectId id
    String name

    static constraints = {
        name(blank: false, unique: true)
    }
}

Book.groovy

package library

import org.bson.types.ObjectId

class Book {

    ObjectId id
    Author author
    String title
    String tag

    static mapping = {
        version false
    }

    static constraints = {
        title(blank: false)
        author(nullable: false)
    }

    def beforeValidate() {
        tag = ("${title} ${author.name}").toLowerCase().split(' ').join('-')
    }
}

PrinceAdeleke avatar Jun 27 '19 15:06 PrinceAdeleke

Voting for this too, seems to be a general problem with saving objects that has "hasMany" since 7.0.0

Tobbe129 avatar Jun 27 '19 18:06 Tobbe129

I have the same problem. I have domain A that hasMany B (B with unique constraint).

when updating A it fails cause is complaining about unique constraint in B.

miguelabautista avatar Aug 27 '19 10:08 miguelabautista

I tried to replicate with Grails 4.0.3 and GORM Mongo 7.0.0. But, it seems to be working fine.

puneetbehl avatar Jul 13 '20 09:07 puneetbehl