Grails
  1. Grails
  2. GRAILS-7652

Constraints are not validated in many-to-many relationship

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 1.3.7
    • Fix Version/s: None
    • Component/s: Persistence
    • Labels:
    • Environment:
      Windows

      Description

      Extending sample from documentation:

      class Book {
         static belongsTo = Author
         static hasMany = [authors:Author]
         String title
      }
      class Author {
         static hasMany = [books:Book]
         String name
      }
      

      we can add constraints e.g.

      class Book {
         static belongsTo = Author
         static hasMany = [authors:Author]
         String title
         static constraints = {
             title blank:false
         }
      }
      class Author {
         static hasMany = [books:Book]
         String name
      }
      

      Now it should not be possible to save an author with a book without title:

      new Author(name:"Stephen King")
      		.addToBooks(new Book(title:''))
      		.save()
      

      as new Book() violates title not blank constraint. But grails does not complain about it. Actually looking at the GrailsDomainClassValidator class we can see that method cascadeToAssociativeProperty does not take care of handling many-to-many relationships at all.

        Activity

        Hide
        Adrian Stachowiak added a comment - - edited

        I have tried to run this with Grails 1.4.0.M1 and the result is event more alarming. Same sample now saves the Author but omits the Book. In the end we have a instance of an Author with a Book in the list of books but the Book is not stored in the database and there is no exception or error raised.

        The example is located at https://github.com/halfdozenshots/GrailsBlogSamples

        Show
        Adrian Stachowiak added a comment - - edited I have tried to run this with Grails 1.4.0.M1 and the result is event more alarming. Same sample now saves the Author but omits the Book. In the end we have a instance of an Author with a Book in the list of books but the Book is not stored in the database and there is no exception or error raised. The example is located at https://github.com/halfdozenshots/GrailsBlogSamples

          People

          • Assignee:
            Unassigned
            Reporter:
            Adrian Stachowiak
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Last Reviewed:

              Development