Problem with unique and nullable properties

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Problem with unique and nullable properties

José Antonio
In Grails 1.1 I have this class:

class Test {
        Integer field1
        Integer field2
       
        static constraints = {
                field1 (nullable:false)
                field2 (nullable:true , unique:'field1')
        }
}

Maybe I'm missing something... but I can add the record (5, null) two times!
I thought the unique constraint would prevent it or
maybe there is a basic explanation for this way, can anybody tell me why?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Problem with unique and nullable properties

a.shneyderman
I think your syntax is off a bit:

unique:true on field 1

?


On Fri, Mar 13, 2009 at 1:46 PM, josant <[hidden email]> wrote:

>
> In Grails 1.1 I have this class:
>
> class Test {
>        Integer field1
>        Integer field2
>
>        static constraints = {
>                field1 (nullable:false)
>                field2 (nullable:true , unique:'field1')
>        }
> }
>
> Maybe I'm missing something... but I can add the record (5, null) two times!
> I thought the unique constraint would prevent it or
> maybe there is a basic explanation for this way, can anybody tell me why?
> --
> View this message in context: http://www.nabble.com/Problem-with-unique-and-nullable-properties-tp22495585p22495585.html
> Sent from the grails - user mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Problem with unique and nullable properties

a.shneyderman
In reply to this post by José Antonio
hmm, no, something else is off here. sorry for opening my mouth :-(

On Fri, Mar 13, 2009 at 1:46 PM, josant <[hidden email]> wrote:

>
> In Grails 1.1 I have this class:
>
> class Test {
>        Integer field1
>        Integer field2
>
>        static constraints = {
>                field1 (nullable:false)
>                field2 (nullable:true , unique:'field1')
>        }
> }
>
> Maybe I'm missing something... but I can add the record (5, null) two times!
> I thought the unique constraint would prevent it or
> maybe there is a basic explanation for this way, can anybody tell me why?
> --
> View this message in context: http://www.nabble.com/Problem-with-unique-and-nullable-properties-tp22495585p22495585.html
> Sent from the grails - user mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Problem with unique and nullable properties

burtbeckwith
In reply to this post by José Antonio
The problem is that NULL isn't a value, it's the lack of a value, so it's not considered for the uniqueness checks.

Burt

>
> In Grails 1.1 I have this class:
>
> class Test {
>         Integer field1
>         Integer field2
>        
>         static constraints = {
> field1 (nullable:false)
>                 field2 (nullable:true , unique:'field1')
>         }
> }
>
> Maybe I'm missing something... but I can add the record (5, null) two times!
> I thought the unique constraint would prevent it or
> maybe there is a basic explanation for this way, can anybody tell me why?

signature.asc (204 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Problem with unique and nullable properties

a.shneyderman
In reply to this post by a.shneyderman
also read the warning in the red box:

http://grails.org/doc/1.0.x/ref/Constraints/unique.html

On Fri, Mar 13, 2009 at 2:00 PM, Alex Shneyderman
<[hidden email]> wrote:

> hmm, no, something else is off here. sorry for opening my mouth :-(
>
> On Fri, Mar 13, 2009 at 1:46 PM, josant <[hidden email]> wrote:
>>
>> In Grails 1.1 I have this class:
>>
>> class Test {
>>        Integer field1
>>        Integer field2
>>
>>        static constraints = {
>>                field1 (nullable:false)
>>                field2 (nullable:true , unique:'field1')
>>        }
>> }
>>
>> Maybe I'm missing something... but I can add the record (5, null) two times!
>> I thought the unique constraint would prevent it or
>> maybe there is a basic explanation for this way, can anybody tell me why?
>> --
>> View this message in context: http://www.nabble.com/Problem-with-unique-and-nullable-properties-tp22495585p22495585.html
>> Sent from the grails - user mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Problem with unique and nullable properties

José Antonio
In reply to this post by a.shneyderman
Hi Alex, I would like to add registries like:

(1,2), (1, null)

but I don't want repeated pairs. With the previous code I can't add (1,2) again (this is good), but app don't prevent me for adding (1,null) many times. How can I avoid this?


Alex Shneyderman wrote
I think your syntax is off a bit:

unique:true on field 1

?


On Fri, Mar 13, 2009 at 1:46 PM, josant <josjim6@gmail.com> wrote:
>
> In Grails 1.1 I have this class:
>
> class Test {
>        Integer field1
>        Integer field2
>
>        static constraints = {
>                field1 (nullable:false)
>                field2 (nullable:true , unique:'field1')
>        }
> }
>
> Maybe I'm missing something... but I can add the record (5, null) two times!
> I thought the unique constraint would prevent it or
> maybe there is a basic explanation for this way, can anybody tell me why?
> --
> View this message in context: http://www.nabble.com/Problem-with-unique-and-nullable-properties-tp22495585p22495585.html
> Sent from the grails - user mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Problem with unique and nullable properties

José Antonio
In reply to this post by burtbeckwith
Many thanks guys, I think I will need to validate this before saving the object, maybe using custom validation, do you think like me?


Burt Beckwith wrote
The problem is that NULL isn't a value, it's the lack of a value, so it's not considered for the uniqueness checks.

Burt

>
> In Grails 1.1 I have this class:
>
> class Test {
>         Integer field1
>         Integer field2
>        
>         static constraints = {
> field1 (nullable:false)
>                 field2 (nullable:true , unique:'field1')
>         }
> }
>
> Maybe I'm missing something... but I can add the record (5, null) two times!
> I thought the unique constraint would prevent it or
> maybe there is a basic explanation for this way, can anybody tell me why?

 
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Problem with unique and nullable properties

a.shneyderman
In reply to this post by José Antonio
On Fri, Mar 13, 2009 at 2:12 PM, josant <[hidden email]> wrote:
>
> Hi Alex, I would like to add registries like:
>
> (1,2), (1, null)
>
> but I don't want repeated pairs. With the previous code I can't add (1,2)
> again (this is good), but app don't prevent me for adding (1,null) many
> times. How can I avoid this?

As Burt mentioned if you allow nulls you can not. But if your
semantics allow it you could probably make one value as a mark value
-1 or 0 and use that instead of null. Then you can have non-null field
and DB will start enforcing the constraint.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Problem with unique and nullable properties

HKL1234
In reply to this post by José Antonio
I used this code to fix it.

field1(nullable:false, unique:['field2'])
field2(nullable:true, validator: { val, obj ->
        if(val == null){
                def code = Test.findByField1(obj.field1)
                        if(code == null){
                                return true
                        }
                        else{
                                return false
                        }
                }
        })
Loading...