Grails
  1. Grails
  2. GRAILS-8656

Unique constraint erroneously finds match when grouped property value is null

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.0 final
    • Fix Version/s: 2.0.1
    • Component/s: None
    • Labels:
      None
    • Patch Submitted:
      Yes
    • Flagged:
      Impediment

      Description

      I have a user class where I would like the user id to be unique for records marked as not deleted
      As a simplification I have the rstriction uniqueness on date deleted (2 deleted records at the same time should also not have the same user id):

      class User {
      String userId
      Date dateDeleted

      static constraints =

      { userId blank: false, unique: 'dateDeleted' dateDeleted nullable: true }

      }

      However, if I save a user with userId = "User1" and a not null date deleted value
      Then validate a 2nd user with userId = "User1" and a null date deleted, the unique constrain kicks in.

      The problem appears in to be in org.grails.datastore.gorm.validation.constraints.UniqueConstraint

      line 69:

       final existing = constraintOwningClass.createCriteria().get {
                          eq constraintPropertyName, propertyValue
                          for(prop in group) {
                              eq prop, target[prop]
                          }
                      }
      

      evaluates to:

       final existing = User.createCriteria().get {
                          eq 'userId', 'User1'
                          eq 'dateDeleted', null
                      }
      

      Which is returning a row, even though the saved user has a not null dateDeleted. This is causing the uniqueness to fire

      1. UserClass.groovy
        0.2 kB
        Neill Robbins
      2. UserClassTests.groovy
        2 kB
        Neill Robbins

        Activity

          People

          • Assignee:
            Graeme Rocher
            Reporter:
            Neill Robbins
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:
              Last Reviewed:

              Development