Grails - 使用 VARCHAR2 字段作为表标识符 - 外键关系失败

发布于 2024-10-15 13:03:35 字数 1040 浏览 8 评论 0原文

我与旧版 Oracle 数据库集成,该数据库使用指定的 VARCHAR2 值作为主键。我正在与这个现有表创建一对多关系。旧表称为“应用程序”(我可能不会更改),新表称为“项目”。许多项目可以分配给一个应用程序。

当 GORM 创建 Project 表时,它会为外键 application_id 创建一个 NUMBER 列,即使这是 Applications 表中的 VARCHAR2 字段。

class Application {
   static hasMany = [projects : Project];  // does not fix problem
   String application_id; 
   ...
   static mapping = {
      table 'applications'
      version false
      id (column:'application_id')
   }
   static constraints = {
      application_id(maxSize:16,blank:false,unique:true,type:"string",generator:"assigned")
   }
   ...
}

class Project {
   Application application;
   ...
}

当我编译应用程序时,我收到如下警告: 不成功:alter table项目添加约束FKED904B1956694CB5外键(application_id) ORA-02267: 列类型与引用的列类型不兼容

当我运行应用程序并单击应用程序控制器时,出现以下错误: SQL状态[99999];错误代码[17059];无法转换为内部表示; 无法转换为内部表示形式

嵌套异常是 java.sql.SQLException:当我单击“项目”| 时 创建我收到此错误: 无法转换为内部表示;嵌套异常是 java.sql.SQLException: 无法转换为 /project/create:172 处的内部表示

那么如何将 Project 类设置为期望应用程序有 VARCHAR2 外键?

感谢您的帮助!

I am integrated with a legacy Oracle database which uses assigned VARCHAR2 values for primary keys. I am creating a one-to-many relationship with this existing table. The legacy table is called Applications (which I may not alter) and the new table is called Projects. Many projects may be assigned to one application.

When GORM creates the Project table it is creating a NUMBER column for the foreign key, application_id, even though this is a VARCHAR2 field in the Applications table.

class Application {
   static hasMany = [projects : Project];  // does not fix problem
   String application_id; 
   ...
   static mapping = {
      table 'applications'
      version false
      id (column:'application_id')
   }
   static constraints = {
      application_id(maxSize:16,blank:false,unique:true,type:"string",generator:"assigned")
   }
   ...
}

class Project {
   Application application;
   ...
}

When I compile the app I get warnings like this:
Unsuccessful: alter table project add constraint FKED904B1956694CB5 foreign key (application_id)
ORA-02267: column type incompatible with referenced column type

When I run the app and click on Application controller I get this error:
SQL state [99999]; error code [17059]; Fail to convert to internal representation; nested exception is java.sql.SQLException: Fail to convert to internal representation

When I click on Project | create I get this error:
Fail to convert to internal representation; nested exception is java.sql.SQLException: Fail to convert to internal representation at /project/create:172

So how can I set the Project class to expect a VARCHAR2 foreign key for the Application?

Thanks for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我ぃ本無心為│何有愛 2024-10-22 13:03:35

请查看这个网站。也许它会对你有所帮助。

Look at this site. Maybe it will help you.

流年已逝 2024-10-22 13:03:35

这是应用程序类中的更正...以防其他人搜索此内容:

class Application {
   static hasMany = [projects : Project];  
   String application_id; 
   String id                  // <--- part of solution

   static mapping = {
      id column:'application_id',generator:'assigned'  // <--- part of solution
   }

   static constraints = {
      application_id(maxSize:16,blank:false,unique:true)  // <--- part of solution
      columns {                                           // <--- part of solution  
                id type:'text'
                application_id type:'text'
              }
   }

Here is the correction in the Application class... in case anyone else searches for this:

class Application {
   static hasMany = [projects : Project];  
   String application_id; 
   String id                  // <--- part of solution

   static mapping = {
      id column:'application_id',generator:'assigned'  // <--- part of solution
   }

   static constraints = {
      application_id(maxSize:16,blank:false,unique:true)  // <--- part of solution
      columns {                                           // <--- part of solution  
                id type:'text'
                application_id type:'text'
              }
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文