我有三個領域類:啤酒,評論和審稿人。如何使用GORM創建組合主鍵?
我想讓Review表在Beer和Reviewer之間創建多對多的關係,所以我希望Review的主鍵是來自Beer和Reviewer的id字段的組合。我遵循這個Grails文檔。
這裏是我的領域類。
class Beer {
String name
String type
Brewery breweryId
static hasMany = [ reviews : Review ]
static constraints = {
}
}
class Reviewer {
String screenName
static hasMany = [ reviews : Review ]
static constraints = {
}
}
class Review implements Serializable {
int score
Beer beer
Reviewer reviewer
static constraints = {
}
static mapping = {
id composite:['beer', 'reviewer']
}
}
我得到編譯錯誤,但這裏對計算器另一個答案說我需要添加implements Serializable
。這照顧了錯誤,但是當我查看數據庫時,我仍然沒有得到一個組合主鍵。
這是我看到表格定義時所看到的。我正在使用Postgres。
Table "public.review"
Column | Type | Modifiers
-------------+---------+-----------
id | bigint | not null
version | bigint | not null
beer_id | bigint | not null
reviewer_id | bigint | not null
score | integer | not null
Indexes:
"review_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
"fkc84ef75823f39326" FOREIGN KEY (beer_id) REFERENCES beer(id)
"fkc84ef7587c483106" FOREIGN KEY (reviewer_id) REFERENCES reviewer(id)
我會很高興只有一個具有唯一約束的複合索引,但我無法弄清楚如何做到這一點。我已經能夠創建一個非唯一的組合索引,但是這有兩個問題。一,它是非獨特的。二,列在索引中按字母順序指定(beer_id,reviewer_id)。我想指定索引中列的順序。