Quantcast

How to set a unique constraint on an association id?

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to set a unique constraint on an association id?

rosenfeld
I didn't find any example in the Grails documentation, but I found a
message in this list explaining how to do what I wanted (kind of):

class Face {
     static hasOne = [nose:Nose]
     Integer position
     static constraints = {
         position unique: 'nose'
     }
}

But looking at the SQL log, I noticed that all fields from Nose are
retrieved from the database when testing for uniqueness.

This is a very expensive operation for my application and I'd like to
write something like this:

position unique: 'noseId' // or position unique: 'nose.id'

Neither of the attempts above did work.

How can I do that? I only want to check for noseId and I don't want to
fetch the whole Nose for checking the uniqueness.

Thanks in advance,

Rodrigo.


---------------------------------------------------------------------
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: How to set a unique constraint on an association id?

Jason Davis
Im confused :s. You want a nose to be associated with one face only?

something like   nose(unique:true) doesnt do what you need?

Thanks

On Sun, Feb 26, 2012 at 11:34 AM, Rodrigo Rosenfeld Rosas
<[hidden email]> wrote:

> I didn't find any example in the Grails documentation, but I found a message
> in this list explaining how to do what I wanted (kind of):
>
> class Face {
>    static hasOne = [nose:Nose]
>    Integer position
>    static constraints = {
>        position unique: 'nose'
>    }
> }
>
> But looking at the SQL log, I noticed that all fields from Nose are
> retrieved from the database when testing for uniqueness.
>
> This is a very expensive operation for my application and I'd like to write
> something like this:
>
> position unique: 'noseId' // or position unique: 'nose.id'
>
> Neither of the attempts above did work.
>
> How can I do that? I only want to check for noseId and I don't want to fetch
> the whole Nose for checking the uniqueness.
>
> Thanks in advance,
>
> Rodrigo.
>
>
> ---------------------------------------------------------------------
> 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: How to set a unique constraint on an association id?

rosenfeld
This is just an example adapted from the Grails documentation, that is
not my real domain class, of course.

Don't try to get any sense from the example, for my real application it
does make sense for the position to be unique across an association id.

I can't expose my application, so let me try another example:

class Reference {
     static belongsTo = [book: Book]
     Integer position
     static constraints = {
         position unique: book
     }
}

I can't have two references with the same position for a single book.

Em 26-02-2012 19:27, Jason Davis escreveu:

> Im confused :s. You want a nose to be associated with one face only?
>
> something like   nose(unique:true) doesnt do what you need?
>
> Thanks
>
> On Sun, Feb 26, 2012 at 11:34 AM, Rodrigo Rosenfeld Rosas
> <[hidden email]>  wrote:
>> I didn't find any example in the Grails documentation, but I found a message
>> in this list explaining how to do what I wanted (kind of):
>>
>> class Face {
>>     static hasOne = [nose:Nose]
>>     Integer position
>>     static constraints = {
>>         position unique: 'nose'
>>     }
>> }
>>
>> But looking at the SQL log, I noticed that all fields from Nose are
>> retrieved from the database when testing for uniqueness.
>>
>> This is a very expensive operation for my application and I'd like to write
>> something like this:
>>
>> position unique: 'noseId' // or position unique: 'nose.id'
>>
>> Neither of the attempts above did work.
>>
>> How can I do that? I only want to check for noseId and I don't want to fetch
>> the whole Nose for checking the uniqueness.
>>
>> Thanks in advance,
>>
>> Rodrigo.
>>
>>


---------------------------------------------------------------------
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: How to set a unique constraint on an association id?

Jason Davis
so you need a composite key?

http://grails.org/doc/latest/guide/GORM.html#compositePrimaryKeys



On Mon, Feb 27, 2012 at 5:19 AM, Rodrigo Rosenfeld Rosas
<[hidden email]> wrote:

> This is just an example adapted from the Grails documentation, that is not
> my real domain class, of course.
>
> Don't try to get any sense from the example, for my real application it does
> make sense for the position to be unique across an association id.
>
> I can't expose my application, so let me try another example:
>
> class Reference {
>    static belongsTo = [book: Book]
>
>    Integer position
>    static constraints = {
>        position unique: book
>    }
> }
>
> I can't have two references with the same position for a single book.
>
> Em 26-02-2012 19:27, Jason Davis escreveu:
>
>> Im confused :s. You want a nose to be associated with one face only?
>>
>> something like   nose(unique:true) doesnt do what you need?
>>
>> Thanks
>>
>> On Sun, Feb 26, 2012 at 11:34 AM, Rodrigo Rosenfeld Rosas
>> <[hidden email]>  wrote:
>>>
>>> I didn't find any example in the Grails documentation, but I found a
>>> message
>>> in this list explaining how to do what I wanted (kind of):
>>>
>>> class Face {
>>>    static hasOne = [nose:Nose]
>>>    Integer position
>>>    static constraints = {
>>>        position unique: 'nose'
>>>    }
>>> }
>>>
>>> But looking at the SQL log, I noticed that all fields from Nose are
>>> retrieved from the database when testing for uniqueness.
>>>
>>> This is a very expensive operation for my application and I'd like to
>>> write
>>> something like this:
>>>
>>> position unique: 'noseId' // or position unique: 'nose.id'
>>>
>>> Neither of the attempts above did work.
>>>
>>> How can I do that? I only want to check for noseId and I don't want to
>>> fetch
>>> the whole Nose for checking the uniqueness.
>>>
>>> Thanks in advance,
>>>
>>> Rodrigo.
>>>
>>>
>
>
> ---------------------------------------------------------------------
> 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: How to set a unique constraint on an association id?

rosenfeld
No, I don't want a composite primary key. I just want position to be
unique among an association id.

Em 27-02-2012 12:58, Jason Davis escreveu:

> so you need a composite key?
>
> http://grails.org/doc/latest/guide/GORM.html#compositePrimaryKeys
>
>
>
> On Mon, Feb 27, 2012 at 5:19 AM, Rodrigo Rosenfeld Rosas
> <[hidden email]>  wrote:
>> This is just an example adapted from the Grails documentation, that is not
>> my real domain class, of course.
>>
>> Don't try to get any sense from the example, for my real application it does
>> make sense for the position to be unique across an association id.
>>
>> I can't expose my application, so let me try another example:
>>
>> class Reference {
>>     static belongsTo = [book: Book]
>>
>>     Integer position
>>     static constraints = {
>>         position unique: book
>>     }
>> }
>>
>> I can't have two references with the same position for a single book.
>>
>> Em 26-02-2012 19:27, Jason Davis escreveu:
>>
>>> Im confused :s. You want a nose to be associated with one face only?
>>>
>>> something like   nose(unique:true) doesnt do what you need?
>>>
>>> Thanks
>>>
>>> On Sun, Feb 26, 2012 at 11:34 AM, Rodrigo Rosenfeld Rosas
>>> <[hidden email]>    wrote:
>>>> I didn't find any example in the Grails documentation, but I found a
>>>> message
>>>> in this list explaining how to do what I wanted (kind of):
>>>>
>>>> class Face {
>>>>     static hasOne = [nose:Nose]
>>>>     Integer position
>>>>     static constraints = {
>>>>         position unique: 'nose'
>>>>     }
>>>> }
>>>>
>>>> But looking at the SQL log, I noticed that all fields from Nose are
>>>> retrieved from the database when testing for uniqueness.
>>>>
>>>> This is a very expensive operation for my application and I'd like to
>>>> write
>>>> something like this:
>>>>
>>>> position unique: 'noseId' // or position unique: 'nose.id'
>>>>
>>>> Neither of the attempts above did work.
>>>>
>>>> How can I do that? I only want to check for noseId and I don't want to
>>>> fetch
>>>> the whole Nose for checking the uniqueness.
>>>>
>>>> Thanks in advance,
>>>>
>>>> Rodrigo.
>>>>
>>>>
>>
>> ---------------------------------------------------------------------
>> 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
>
>
>


---------------------------------------------------------------------
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: How to set a unique constraint on an association id?

Jason Davis
wouldn't a composite key of book and position enforce exactly that?

On Mon, Feb 27, 2012 at 11:30 AM, Rodrigo Rosenfeld Rosas
<[hidden email]> wrote:

> No, I don't want a composite primary key. I just want position to be unique
> among an association id.
>
> Em 27-02-2012 12:58, Jason Davis escreveu:
>
>> so you need a composite key?
>>
>> http://grails.org/doc/latest/guide/GORM.html#compositePrimaryKeys
>>
>>
>>
>> On Mon, Feb 27, 2012 at 5:19 AM, Rodrigo Rosenfeld Rosas
>> <[hidden email]>  wrote:
>>>
>>> This is just an example adapted from the Grails documentation, that is
>>> not
>>> my real domain class, of course.
>>>
>>> Don't try to get any sense from the example, for my real application it
>>> does
>>> make sense for the position to be unique across an association id.
>>>
>>> I can't expose my application, so let me try another example:
>>>
>>> class Reference {
>>>    static belongsTo = [book: Book]
>>>
>>>    Integer position
>>>    static constraints = {
>>>        position unique: book
>>>    }
>>> }
>>>
>>> I can't have two references with the same position for a single book.
>>>
>>> Em 26-02-2012 19:27, Jason Davis escreveu:
>>>
>>>> Im confused :s. You want a nose to be associated with one face only?
>>>>
>>>> something like   nose(unique:true) doesnt do what you need?
>>>>
>>>> Thanks
>>>>
>>>> On Sun, Feb 26, 2012 at 11:34 AM, Rodrigo Rosenfeld Rosas
>>>> <[hidden email]>    wrote:
>>>>>
>>>>> I didn't find any example in the Grails documentation, but I found a
>>>>> message
>>>>> in this list explaining how to do what I wanted (kind of):
>>>>>
>>>>> class Face {
>>>>>    static hasOne = [nose:Nose]
>>>>>    Integer position
>>>>>    static constraints = {
>>>>>        position unique: 'nose'
>>>>>    }
>>>>> }
>>>>>
>>>>> But looking at the SQL log, I noticed that all fields from Nose are
>>>>> retrieved from the database when testing for uniqueness.
>>>>>
>>>>> This is a very expensive operation for my application and I'd like to
>>>>> write
>>>>> something like this:
>>>>>
>>>>> position unique: 'noseId' // or position unique: 'nose.id'
>>>>>
>>>>> Neither of the attempts above did work.
>>>>>
>>>>> How can I do that? I only want to check for noseId and I don't want to
>>>>> fetch
>>>>> the whole Nose for checking the uniqueness.
>>>>>
>>>>> Thanks in advance,
>>>>>
>>>>> Rodrigo.
>>>>>
>>>>>
>>>
>>> ---------------------------------------------------------------------
>>> 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
>>
>>
>>
>
>
> ---------------------------------------------------------------------
> 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: How to set a unique constraint on an association id?

rosenfeld
I think you're offering a workaround over an existing limitation of GORM, not a real solution.

Primary key has a different concern than a unique index. I still want the primary key to be a unique id, but I also have other unique indexes in the database that I'd like to enforce the constraint in the application level too.

With ActiveRecord in Rails you can do that with the examples below, as documented in the following link:

http://ar.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M000086

validates_uniqueness_of :user_name, :scope => :account_id

or

validates_uniqueness_of :teacher_id, :scope => [:semester_id, :class_id]

And you can have also other scoped unique constraints for the same model. You can't have multiple unique constraints  and set all of them with composite keys for example.

But I have other performance considerations in mind and justifications why I never use composite keys in relational databases, but I won't discuss them here as they are not relevant to this issue.

Please, stop trying to convince me to adopt a composite primary key. Just let me know if you know how I can just set the unique constraint with a scoped association.

Thanks,
Rodrigo.

Em 27-02-2012 15:35, Jason Davis escreveu:
wouldn't a composite key of book and position enforce exactly that?

On Mon, Feb 27, 2012 at 11:30 AM, Rodrigo Rosenfeld Rosas
[hidden email] wrote:
No, I don't want a composite primary key. I just want position to be unique
among an association id.

Em 27-02-2012 12:58, Jason Davis escreveu:

so you need a composite key?

http://grails.org/doc/latest/guide/GORM.html#compositePrimaryKeys



On Mon, Feb 27, 2012 at 5:19 AM, Rodrigo Rosenfeld Rosas
[hidden email]  wrote:
This is just an example adapted from the Grails documentation, that is
not
my real domain class, of course.

Don't try to get any sense from the example, for my real application it
does
make sense for the position to be unique across an association id.

I can't expose my application, so let me try another example:

class Reference {
   static belongsTo = [book: Book]

   Integer position
   static constraints = {
       position unique: book
   }
}

I can't have two references with the same position for a single book.

Em 26-02-2012 19:27, Jason Davis escreveu:

Im confused :s. You want a nose to be associated with one face only?

something like   nose(unique:true) doesnt do what you need?

Thanks

On Sun, Feb 26, 2012 at 11:34 AM, Rodrigo Rosenfeld Rosas
[hidden email]    wrote:
I didn't find any example in the Grails documentation, but I found a
message
in this list explaining how to do what I wanted (kind of):

class Face {
   static hasOne = [nose:Nose]
   Integer position
   static constraints = {
       position unique: 'nose'
   }
}

But looking at the SQL log, I noticed that all fields from Nose are
retrieved from the database when testing for uniqueness.

This is a very expensive operation for my application and I'd like to
write
something like this:

position unique: 'noseId' // or position unique: 'nose.id'

Neither of the attempts above did work.

How can I do that? I only want to check for noseId and I don't want to
fetch
the whole Nose for checking the uniqueness.

Thanks in advance,

Rodrigo.




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

Re: How to set a unique constraint on an association id?

Jason Davis
I wasnt trying to convince you of anything, I have nothing to sell here....

Just trying to understand your problem. Wont happen again :)



On Mon, Feb 27, 2012 at 12:15 PM, Rodrigo Rosenfeld Rosas
<[hidden email]> wrote:

> I think you're offering a workaround over an existing limitation of GORM,
> not a real solution.
>
> Primary key has a different concern than a unique index. I still want the
> primary key to be a unique id, but I also have other unique indexes in the
> database that I'd like to enforce the constraint in the application level
> too.
>
> With ActiveRecord in Rails you can do that with the examples below, as
> documented in the following link:
>
> http://ar.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M000086
>
> validates_uniqueness_of :user_name, :scope => :account_id
>
> or
>
> validates_uniqueness_of :teacher_id, :scope => [:semester_id, :class_id]
>
> And you can have also other scoped unique constraints for the same model.
> You can't have multiple unique constraints  and set all of them with
> composite keys for example.
>
> But I have other performance considerations in mind and justifications why I
> never use composite keys in relational databases, but I won't discuss them
> here as they are not relevant to this issue.
>
> Please, stop trying to convince me to adopt a composite primary key. Just
> let me know if you know how I can just set the unique constraint with a
> scoped association.
>
> Thanks,
> Rodrigo.
>
> Em 27-02-2012 15:35, Jason Davis escreveu:
>
> wouldn't a composite key of book and position enforce exactly that?
>
> On Mon, Feb 27, 2012 at 11:30 AM, Rodrigo Rosenfeld Rosas
> <[hidden email]> wrote:
>
> No, I don't want a composite primary key. I just want position to be unique
> among an association id.
>
> Em 27-02-2012 12:58, Jason Davis escreveu:
>
> so you need a composite key?
>
> http://grails.org/doc/latest/guide/GORM.html#compositePrimaryKeys
>
>
>
> On Mon, Feb 27, 2012 at 5:19 AM, Rodrigo Rosenfeld Rosas
> <[hidden email]>  wrote:
>
> This is just an example adapted from the Grails documentation, that is
> not
> my real domain class, of course.
>
> Don't try to get any sense from the example, for my real application it
> does
> make sense for the position to be unique across an association id.
>
> I can't expose my application, so let me try another example:
>
> class Reference {
>    static belongsTo = [book: Book]
>
>    Integer position
>    static constraints = {
>        position unique: book
>    }
> }
>
> I can't have two references with the same position for a single book.
>
> Em 26-02-2012 19:27, Jason Davis escreveu:
>
> Im confused :s. You want a nose to be associated with one face only?
>
> something like   nose(unique:true) doesnt do what you need?
>
> Thanks
>
> On Sun, Feb 26, 2012 at 11:34 AM, Rodrigo Rosenfeld Rosas
> <[hidden email]>    wrote:
>
> I didn't find any example in the Grails documentation, but I found a
> message
> in this list explaining how to do what I wanted (kind of):
>
> class Face {
>    static hasOne = [nose:Nose]
>    Integer position
>    static constraints = {
>        position unique: 'nose'
>    }
> }
>
> But looking at the SQL log, I noticed that all fields from Nose are
> retrieved from the database when testing for uniqueness.
>
> This is a very expensive operation for my application and I'd like to
> write
> something like this:
>
> position unique: 'noseId' // or position unique: 'nose.id'
>
> Neither of the attempts above did work.
>
> How can I do that? I only want to check for noseId and I don't want to
> fetch
> the whole Nose for checking the uniqueness.
>
> Thanks in advance,
>
> Rodrigo.
>
>
>
>

---------------------------------------------------------------------
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: How to set a unique constraint on an association id?

Shane Petroff
In reply to this post by rosenfeld
Are you just looking for something like:

static constraints = {
    ...
    login(unique:['group','department'])
    ...
}

from

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

On 2/27/2012 1:15 PM, Rodrigo Rosenfeld Rosas wrote:
I think you're offering a workaround over an existing limitation of GORM, not a real solution.

Primary key has a different concern than a unique index. I still want the primary key to be a unique id, but I also have other unique indexes in the database that I'd like to enforce the constraint in the application level too.

With ActiveRecord in Rails you can do that with the examples below, as documented in the following link:

http://ar.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M000086

validates_uniqueness_of :user_name, :scope => :account_id

or

validates_uniqueness_of :teacher_id, :scope => [:semester_id, :class_id]

And you can have also other scoped unique constraints for the same model. You can't have multiple unique constraints  and set all of them with composite keys for example.

But I have other performance considerations in mind and justifications why I never use composite keys in relational databases, but I won't discuss them here as they are not relevant to this issue.

Please, stop trying to convince me to adopt a composite primary key. Just let me know if you know how I can just set the unique constraint with a scoped association.

Thanks,
Rodrigo.

Em 27-02-2012 15:35, Jason Davis escreveu:
wouldn't a composite key of book and position enforce exactly that?

On Mon, Feb 27, 2012 at 11:30 AM, Rodrigo Rosenfeld Rosas
[hidden email] wrote:
No, I don't want a composite primary key. I just want position to be unique
among an association id.

Em 27-02-2012 12:58, Jason Davis escreveu:

so you need a composite key?

http://grails.org/doc/latest/guide/GORM.html#compositePrimaryKeys



On Mon, Feb 27, 2012 at 5:19 AM, Rodrigo Rosenfeld Rosas
[hidden email]  wrote:
This is just an example adapted from the Grails documentation, that is
not
my real domain class, of course.

Don't try to get any sense from the example, for my real application it
does
make sense for the position to be unique across an association id.

I can't expose my application, so let me try another example:

class Reference {
   static belongsTo = [book: Book]

   Integer position
   static constraints = {
       position unique: book
   }
}

I can't have two references with the same position for a single book.

Em 26-02-2012 19:27, Jason Davis escreveu:

Im confused :s. You want a nose to be associated with one face only?

something like   nose(unique:true) doesnt do what you need?

Thanks

On Sun, Feb 26, 2012 at 11:34 AM, Rodrigo Rosenfeld Rosas
[hidden email]    wrote:
I didn't find any example in the Grails documentation, but I found a
message
in this list explaining how to do what I wanted (kind of):

class Face {
   static hasOne = [nose:Nose]
   Integer position
   static constraints = {
       position unique: 'nose'
   }
}

But looking at the SQL log, I noticed that all fields from Nose are
retrieved from the database when testing for uniqueness.

This is a very expensive operation for my application and I'd like to
write
something like this:

position unique: 'noseId' // or position unique: 'nose.id'

Neither of the attempts above did work.

How can I do that? I only want to check for noseId and I don't want to
fetch
the whole Nose for checking the uniqueness.

Thanks in advance,

Rodrigo.






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

Re: How to set a unique constraint on an association id?

rosenfeld
In reply to this post by Jason Davis
Just to make it clear, I didn't intend to be rude in my previous
message. Sorry if it sounded so.

I really appreciate your help.

Thanks,
Rodrigo.

Em 27-02-2012 16:29, Jason Davis escreveu:

> I wasnt trying to convince you of anything, I have nothing to sell here....
>
> Just trying to understand your problem. Wont happen again :)
>
>
>
> On Mon, Feb 27, 2012 at 12:15 PM, Rodrigo Rosenfeld Rosas
> <[hidden email]>  wrote:
>> I think you're offering a workaround over an existing limitation of GORM,
>> not a real solution.
>>
>> Primary key has a different concern than a unique index. I still want the
>> primary key to be a unique id, but I also have other unique indexes in the
>> database that I'd like to enforce the constraint in the application level
>> too.
>>
>> With ActiveRecord in Rails you can do that with the examples below, as
>> documented in the following link:
>>
>> http://ar.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M000086
>>
>> validates_uniqueness_of :user_name, :scope =>  :account_id
>>
>> or
>>
>> validates_uniqueness_of :teacher_id, :scope =>  [:semester_id, :class_id]
>>
>> And you can have also other scoped unique constraints for the same model.
>> You can't have multiple unique constraints  and set all of them with
>> composite keys for example.
>>
>> But I have other performance considerations in mind and justifications why I
>> never use composite keys in relational databases, but I won't discuss them
>> here as they are not relevant to this issue.
>>
>> Please, stop trying to convince me to adopt a composite primary key. Just
>> let me know if you know how I can just set the unique constraint with a
>> scoped association.
>>
>> Thanks,
>> Rodrigo.
>>
>> Em 27-02-2012 15:35, Jason Davis escreveu:
>>
>> wouldn't a composite key of book and position enforce exactly that?
>>
>> On Mon, Feb 27, 2012 at 11:30 AM, Rodrigo Rosenfeld Rosas
>> <[hidden email]>  wrote:
>>
>> No, I don't want a composite primary key. I just want position to be unique
>> among an association id.
>>
>> Em 27-02-2012 12:58, Jason Davis escreveu:
>>
>> so you need a composite key?
>>
>> http://grails.org/doc/latest/guide/GORM.html#compositePrimaryKeys
>>
>>
>>
>> On Mon, Feb 27, 2012 at 5:19 AM, Rodrigo Rosenfeld Rosas
>> <[hidden email]>    wrote:
>>
>> This is just an example adapted from the Grails documentation, that is
>> not
>> my real domain class, of course.
>>
>> Don't try to get any sense from the example, for my real application it
>> does
>> make sense for the position to be unique across an association id.
>>
>> I can't expose my application, so let me try another example:
>>
>> class Reference {
>>     static belongsTo = [book: Book]
>>
>>     Integer position
>>     static constraints = {
>>         position unique: book
>>     }
>> }
>>
>> I can't have two references with the same position for a single book.
>>
>> Em 26-02-2012 19:27, Jason Davis escreveu:
>>
>> Im confused :s. You want a nose to be associated with one face only?
>>
>> something like   nose(unique:true) doesnt do what you need?
>>
>> Thanks
>>
>> On Sun, Feb 26, 2012 at 11:34 AM, Rodrigo Rosenfeld Rosas
>> <[hidden email]>      wrote:
>>
>> I didn't find any example in the Grails documentation, but I found a
>> message
>> in this list explaining how to do what I wanted (kind of):
>>
>> class Face {
>>     static hasOne = [nose:Nose]
>>     Integer position
>>     static constraints = {
>>         position unique: 'nose'
>>     }
>> }
>>
>> But looking at the SQL log, I noticed that all fields from Nose are
>> retrieved from the database when testing for uniqueness.
>>
>> This is a very expensive operation for my application and I'd like to
>> write
>> something like this:
>>
>> position unique: 'noseId' // or position unique: 'nose.id'
>>
>> Neither of the attempts above did work.
>>
>> How can I do that? I only want to check for noseId and I don't want to
>> fetch
>> the whole Nose for checking the uniqueness.
>>
>> Thanks in advance,
>>
>> Rodrigo.
>>


---------------------------------------------------------------------
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: How to set a unique constraint on an association id?

rosenfeld
In reply to this post by Shane Petroff
Hi Shane, thanks me for pointing me out where this is documented. I couldn't find it before but now I see the "Constraints" section in the right side.

But this doesn't solve the issue to me due to performance issues. Please take a look at my first message in this thread or at this JIRA I created earlier today:

http://jira.grails.org/browse/GRAILS-8850

Thanks,
Rodrigo.

Em 27-02-2012 16:52, Shane Petroff escreveu:
Are you just looking for something like:

static constraints = {
    ...
    login(unique:['group','department'])
    ...
}

from

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

On 2/27/2012 1:15 PM, Rodrigo Rosenfeld Rosas wrote:
I think you're offering a workaround over an existing limitation of GORM, not a real solution.

Primary key has a different concern than a unique index. I still want the primary key to be a unique id, but I also have other unique indexes in the database that I'd like to enforce the constraint in the application level too.

With ActiveRecord in Rails you can do that with the examples below, as documented in the following link:

http://ar.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M000086

validates_uniqueness_of :user_name, :scope => :account_id

or

validates_uniqueness_of :teacher_id, :scope => [:semester_id, :class_id]

And you can have also other scoped unique constraints for the same model. You can't have multiple unique constraints  and set all of them with composite keys for example.

But I have other performance considerations in mind and justifications why I never use composite keys in relational databases, but I won't discuss them here as they are not relevant to this issue.

Please, stop trying to convince me to adopt a composite primary key. Just let me know if you know how I can just set the unique constraint with a scoped association.

Thanks,
Rodrigo.

Em 27-02-2012 15:35, Jason Davis escreveu:
wouldn't a composite key of book and position enforce exactly that?

On Mon, Feb 27, 2012 at 11:30 AM, Rodrigo Rosenfeld Rosas
[hidden email] wrote:
No, I don't want a composite primary key. I just want position to be unique
among an association id.

Em 27-02-2012 12:58, Jason Davis escreveu:

so you need a composite key?

http://grails.org/doc/latest/guide/GORM.html#compositePrimaryKeys



On Mon, Feb 27, 2012 at 5:19 AM, Rodrigo Rosenfeld Rosas
[hidden email]  wrote:
This is just an example adapted from the Grails documentation, that is
not
my real domain class, of course.

Don't try to get any sense from the example, for my real application it
does
make sense for the position to be unique across an association id.

I can't expose my application, so let me try another example:

class Reference {
   static belongsTo = [book: Book]

   Integer position
   static constraints = {
       position unique: book
   }
}

I can't have two references with the same position for a single book.

Em 26-02-2012 19:27, Jason Davis escreveu:

Im confused :s. You want a nose to be associated with one face only?

something like   nose(unique:true) doesnt do what you need?

Thanks

On Sun, Feb 26, 2012 at 11:34 AM, Rodrigo Rosenfeld Rosas
[hidden email]    wrote:
I didn't find any example in the Grails documentation, but I found a
message
in this list explaining how to do what I wanted (kind of):

class Face {
   static hasOne = [nose:Nose]
   Integer position
   static constraints = {
       position unique: 'nose'
   }
}

But looking at the SQL log, I noticed that all fields from Nose are
retrieved from the database when testing for uniqueness.

This is a very expensive operation for my application and I'd like to
write
something like this:

position unique: 'noseId' // or position unique: 'nose.id'

Neither of the attempts above did work.

How can I do that? I only want to check for noseId and I don't want to
fetch
the whole Nose for checking the uniqueness.

Thanks in advance,

Rodrigo.






-- 
Shane

Loading...