[英]How to map two tables and not make any changes when mapping legacy database tables in Grails?
我是Grails和Mapping的新手,我有類似的東西。 我有兩個域類,我需要在它們之間建立關系,完成關系后,不會對PostgreSQL數據庫中的現有表進行任何更改。
class Insurance{
Integer id
String osg_name
String osg_logo
String osg_email
String osg_link
static hasMany = [ insurancePackage: InsurancePackage]
static constraints = {
id(blank: false)
osg_name (blank: false, size: 0..155)
osg_logo (size: 0..155)
osg_email (blank: false, size: 0..100)
osg_link (size: 0..155)
}
static mapping = {
table name: "insurance", schema: "common"
version false
id generator :'identity', column :'osg_id', type:'integer'
}
}
class InsurancePackage{
Integer id
Integer osg_id
String osgp_comment
Integer tpo_id
String osgp_link
String osgp_label
//static belongsTo = Insurance
static belongsTo = [insurance: Insurance]
static constraints = {
id(blank: false)
osg_id (blank: false)
osgp_comment (blank: false, size: 0..500)
tpo_id (blank: false,)
osgp_link (blank: false, size: 0..155)
osgp_label (blank: false, size: 0..10)
}
static mapping = {
table name: "insurance_package", schema: 'common'
version false
id generator :'identity', column :'osgp_id', type:'integer'
}
}
這是我得到的錯誤
Error 2015-07-16 13:38:49,845 [localhost-startStop-1] ERROR hbm2ddl.SchemaUpdate - Unsuccessful: alter table revoco.insurance_package add column insurance_id int4 not null
| Error 2015-07-16 13:38:49,845 [localhost-startStop-1] ERROR hbm2ddl.SchemaUpdate - ERROR: column "insurance_id " contains null values
| Error 2015-07-16 13:38:49,845 [localhost-startStop-1] ERROR hbm2ddl.SchemaUpdate - Unsuccessful: alter table revoco.insurance_package add constraint FK684953517A89512C foreign key (insurance_id ) references revoco.insurance
| Error 2015-07-16 13:38:49,845 [localhost-startStop-1] ERROR hbm2ddl.SchemaUpdate - ERROR: column "insurance_id " referenced in foreign key constraint does not exist
因此,我無法連接兩個表,並且遇到相同的錯誤,出於某種原因,Grails試圖找到insurance_id,但是在類中沒有定義,並且他們試圖更改我的表,並且我不希望這種情況發生。
您將在insurance_package表中創建一個新列,其中包含保險表的外鍵。 (hasMany和belongsTo->一對多)這里的問題是,該列默認情況下具有NOT NULL約束,但是表似乎已經包含數據。
現在的問題是:如何處理表中已包含的數據。 Grails想要設置NOT NULL約束,但是不能設置,因為那里已經有約束並且因為您剛剛創建了列並且值是NULL
根據您的用例,您有3種選擇:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.