如何更改用于在 Grails 中映射关联的外键名称
我正在尝试使用 Grails 启动并运行一个项目。 我有 2 个表,areaserver 和 serverprotocol。 区域服务器与服务器协议具有一对多关系(一个区域服务器到多个服务器协议)。 实体类 AreaServer 有一个指定的字符串主键。 我已经手动将 AreaServer 的 id 键配置为分配的字符串, 我已经将 ServerProtocol 类配置为 AreaServer 的多对一关系中的许多类,但是当我尝试在 AreaServer 的“映射”部分中手动设置外键时,Grails 使用传统方法,假设 AreaServer 的外键为一个值为“id”的自动递增 id
这是我的 AreaServer 实体类:
class AreaServer {
String serverId
...
...
static hasMany = [ serverProtocol : ServerProtocol ]
static mapping = {
serverProtocol lazy:false
id name: 'serverId', generator: 'assigned'
serverProtocol column: 'server_serverId'
}
}
这是我的 ServerProtocol 实体类:
class ServerProtocol {
long dbId
....
....
static belongsTo = [ areaServer : AreaServer ]
static mapping = {
id name: 'dbId'
version false
}
}
当 Grails 创建数据库并创建“serverprotocol”表时有一个名为“areaServer_id”而不是“server_serverId”的外键,我在 AreaServer 的映射部分将其配置为执行此操作。 像我尝试做的那样配置外键应该是一个标准过程,如 Grails.org 文档中的“表和名称列”所示 http://grails.org/doc/latest/guide/5.%20Object%20Relational%20Mapping%20%28GORM%29.html 由于某种原因,更改外键名称(当外键嵌套在一对多关系的“多”表中时)不起作用,文档说它应该起作用。
我知道 Grails 标准约定是期望每个表都有一个名为“id”的自动递增主键,这就是为什么外键被命名为“areaServer_id”(因为 AreaServer 在 ServerProtocol 中被引用为 areaServer)。 有谁知道为什么 Grails 不允许我像 Grails 文档中所说的那样手动修改外键的名称?
感谢您提前提供的任何帮助!
I'm trying to get a project up and running using Grails.
I have 2 tables, areaserver and serverprotocol.
areaserver has a one-to-many relationship with serverprotocol (one areaserver to many serverprotocols).
The entity class AreaServer has an assigned string primary key.
I've manually configured the AreaServer's id key to be an assigned string,
and I've configured the ServerProtocol class as the many in the AreaServer's many-to-one relationship, but when I try and set the foreign key manually in AreaServer's "mapping" section, Grails uses the conventional approach where it assumes AreaServer's foreign key is an auto-incrementing id with value "id"
Heres my AreaServer entity class:
class AreaServer {
String serverId
...
...
static hasMany = [ serverProtocol : ServerProtocol ]
static mapping = {
serverProtocol lazy:false
id name: 'serverId', generator: 'assigned'
serverProtocol column: 'server_serverId'
}
}
Here is my ServerProtocol entity class:
class ServerProtocol {
long dbId
....
....
static belongsTo = [ areaServer : AreaServer ]
static mapping = {
id name: 'dbId'
version false
}
}
When Grails creates the database and tables the "serverprotocol" table has a foreign key that is named "areaServer_id" instead of "server_serverId" which I configured it to do in AreaServer's mapping section.
Configuring the foreign key like I am attempting to do is supposed to be a standard procedure as shown under "table and name columns" in the Grails.org document http://grails.org/doc/latest/guide/5.%20Object%20Relational%20Mapping%20%28GORM%29.html
For some reason altering the foreign key name (when the foreign key is nested within the "many" table of the one-to-many relationship) just doesn't work, which the documentation says that it should.
I know that Grails standard convention is to expect each table to have an auto-incrementing primary key called "id", and that is why the foreign key is being named "areaServer_id" (since AreaServer is referenced as areaServer in ServerProtocol).
Does anyone know why Grails wouldnt allow me to modify the foreign key's name manually like it says is possible in the Grails's documentation?
Thank you for any help in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这样的事情可能会起作用:
Something like this may work I think: