【问题标题】:How to map two tables and not make any changes when mapping legacy database tables in Grails?在 Grails 中映射遗留数据库表时如何映射两个表而不进行任何更改?
【发布时间】:2015-07-16 23:22:43
【问题描述】:

我是 Grails 和映射的新手,我有一些看起来像这样的东西。 我有两个域类,我需要在它们之间建立关系,当关系完成时,不会对我的 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,但它没有在类中定义,他们试图改变我的表,我不希望那样即将发生。

【问题讨论】:

  • 当您对我的回答感到满意时,是否介意将我的回答标记为正确?

标签: database postgresql grails mapping legacy-database


【解决方案1】:

您在 insurance_package 表中创建了一个新列,该列包含保险表的外键。 (hasMany 和 belongsTo --> 一对多) 这里的问题是该列默认情况下具有 NOT NULL 约束,但表中似乎已经有数据。

现在的问题是:如何处理表中已包含的数据。 Grails 想要设置 NOT NULL 约束但不能,因为那里已经存在并且因为您刚刚创建了列并且值为 NULL

根据您的用例,您有 3 个选项:

  1. 删除表中已包含的值(可能不需要)
  2. 进入数据库管理工具并为这些行设置外键,然后重新启动服务器。错误应该会消失
  3. 将“InsurancePackage”对象中保险参考 (belongsTo) 的约束设置为可为空:true

【讨论】:

  • 只是通过您给我的选项,1. 我不能删除表中的数据,因为我的数据需要保持原样。 2.与第一个相同的方式我无法更改数据库,3.当我在类 InsurancePackage 中声明保险保险并放入约束保险时(可为空:true)我没有收到错误但我的 InsurancePackage 表已更改并且我有新列称为 insurance_id,我不想要那个。我的 Insurance 和 InsurancePackage 表有 osg_id 列,我正在尝试通过该 FK 连接两个表,以便我的数据和表看起来相同。
  • 我了解选项 1 和 2 不是您想要的。我不明白的是你有一个新专栏。该列应该已经存在。您需要有一列才能在 insurance 和 insurance_package 之间建立 FK 连接(一对多)。具有可为空的 true 或 false 之间的唯一区别是,列 (unsurance_id) 可以保存空值或不能保存空值。然后请编辑您的问题,并向我们提供您目前的数据库结构以及您想要实现的目标
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-25
  • 2012-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多