Tran Tam 14
Tran Tam 14

Reputation: 75

Grails unique constraint fail to update

I know that this issue related to other posts, but none of them solve my problem.

I'm using Grails 2.5.1, using oracle database.

I got a domain class CardMaster. It contains user_id, card_no and password.

In constrain I define card_no is unique by user_id.

Then I'd like to use user_id as a Primary Key, so I map id by:

id name:'user_id', generator: 'assigned'

However, when I find and update record by findByUser_id. I got a error. It says that the card_no must be unique and fail to save.

I read other posts and they say that alter primary key is seems to be not mapped as id. If I change mapping by:

id column:'user_id', generator: 'assigned'

it successfully saves but in table, 2 column named user_id appear, 1 presents for user_id and other for id.

Any solutions will be appreciated.

Thanks.

Update

Here is the full code for domain class:

class CardUserMaster {
    String card_no
    String user_id
    String password
  static constraints = {
    card_no(nullable: false, blank: false, maxSize: 19, unique: 'user_id')
    user_id(nullable: false, blank: false, maxSize: 10, minSize: 6)
    password(nullable: false, blank: false, maxSize: 32, minSize: 8)
  static mapping = {
        table('CARDUSER_MASTER')
        id name: 'user_id'
        version false
        id generator: 'assigned'
    }
}

Upvotes: 0

Views: 756

Answers (1)

Rahul Babu
Rahul Babu

Reputation: 790

Update you mappings as :

static mapping = {
        table('CARDUSER_MASTER')
        id column:"user_id", name: 'user_id', generator: 'assigned'
        version false
    }

We need to provide the actual column name from db for which the primary key is referring to as well as the custom name for the primary key which we want together.

Upvotes: 1

Related Questions