GORM 映射:使索引唯一

发布于 2024-10-29 11:37:24 字数 959 浏览 5 评论 0原文

我今天感觉有点慢。我正在尝试做一些我认为很简单的事情。我有一个 Domain 类,其属性名为“name”。我希望“名称”有一个索引,并且我希望索引要求“名称”是唯一的。我已经设置了唯一约束并尝试创建索引。对于如何将唯一属性添加到索引,我无法理解 Gorm 文档。这是一些代码:

class Project {
    String name

    static hasMany = [things:Things]

    static mapping = {
        name index:'name_idx'
    }
    static constraints = {
        name(unique:true)
    }
}

上面的一切都很好,除了在 mysql 中执行“显示项目索引”时,它显示我的名称键不唯一。我知道问题是我没有在映射中指定唯一性,但坦率地说,gorm 的文档让我很头疼。我看到了有关列的各种内容,但我在网络上找不到任何可以显示我想要执行的操作的示例。我不需要复杂的映射或复合键,我只想知道将唯一属性添加到上面的映射声明的语法。欢迎任何建议。

我还做了一个 grails 导出模式,并看到以下内容:

create index name_idx on project (name);

没有任何内容表明该索引需要唯一值

相关的后续问题是,一旦我成功使该索引唯一,当我去保存时,我应该期望什么类型的错误项目实例且名称不唯一?是否有抛出特定异常?我意识到,即使我检查给定的“名称”是否唯一,当我保存它时,仍然有可能存在带有该名称的行。

我很确定执行我想要的操作的语法很简单,但我只是找不到一个简单的示例来进行自我教育。我去过页面,但它没有解释如何强制执行唯一性。我想在名称索引级别强制执行它。

I'm feeling a little slow today. I'm trying to do something that I think is very simple. I have a Domain class with a property called 'name'. I want 'name' to have an index, and I want the index to require that the 'name' is unique. I've set the unique constraint and tried creating an index. I can't make sense out of the Gorm docs as to how I add the unique attribute to the index. Here's some code:

class Project {
    String name

    static hasMany = [things:Things]

    static mapping = {
        name index:'name_idx'
    }
    static constraints = {
        name(unique:true)
    }
}

All is well with the above, except when do "show indexes from project" in mysql it shows my name key as not unique. I know the problem is that I am not specifying unique in the mapping, but quite frankly the docs for gorm are making my head hurt. I see all kinds of stuff about columns, but I can't find a single example anywhere on the web that shows what I want to do. I don't need complex mappings or compound keys, I just want to know the syntax to add the unique attribute to the mapping declaration above. Any advice welcome.

I also did a grails export-schema and see the following:

create index name_idx on project (name);

Nothing in that to indicate this index requires unique values

A related followup question would be once I succeed in making that index unique, what type of error should I expect when I go to save a Project instance and the name is not unique? Is there a specific exception thrown? I realize that even if I check that a given 'name' is unique there's still a possibility that by the time I save it there may be a row with that name.

I'm quite sure the syntax to do what I want is simple but I just can't find a simple example to educate myself with. I've been to this page but it doesn't explain HOW the uniqueness is enforced. I'd like to enforce it at the name index level.

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

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

发布评论

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

评论(2

爱本泡沫多脆弱 2024-11-05 11:37:24

indexColumn 允许配置其他选项。这可能就是您正在寻找的。

static mapping = {
    name indexColumn:[name:'name_idx', unique:true]
}

indexColumn 的 Grails 文档

The indexColumn allows additional options to be configured. This may be what you're looking for.

static mapping = {
    name indexColumn:[name:'name_idx', unique:true]
}

Grails Documentation for indexColumn

抠脚大汉 2024-11-05 11:37:24

如果您仅设置唯一约束,GORM 会发送 DDL 在数据库上创建唯一索引。

static constraints = {
   name nullable: false, unique: true
} 

If you put only the unique constraint the GORM send DDL to create an unique index on database.

static constraints = {
   name nullable: false, unique: true
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文