Grails unique constraint fail to update

时间:2015-09-14 15:19:20

标签: grails gorm unique-constraint

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'
    }
}

1 个答案:

答案 0 :(得分:1)

将映射更新为:

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

我们需要提供主键所引用的db的实际列名以及我们想要的主键的自定义名称。

相关问题