【问题标题】:Grails/MySQL field length constraintGrails/MySQL 字段长度约束
【发布时间】:2012-06-08 00:38:13
【问题描述】:

我正在使用 Grails 1.3.7 和 MySQL 5.1.5.5。我有一个定义这样的字符串属性的类:

class Topic {
    String name;
    User owner;
    // other fields

    static constraints = {
        name(blank: false, maxSize: 1024, unique: ['owner'])
    }
}

这适用于内存数据库,但是当我部署 WAR 文件时,我收到此错误:

12/06/07 17:09:48 ERROR hbm2ddl.SchemaUpdate: Unsuccessful: create table topic (id bigint not null auto_increment, version bigint not null, date_created datetime not null, description longtext, name longtext not null, owner_id bigint not null, primary key (id), unique (owner_id, name))
12/06/07 17:09:48 ERROR hbm2ddl.SchemaUpdate: BLOB/TEXT column 'name' used in key specification without a key length

如果我将 maxSize 规格更改为 255,则一切正常。

我对文档的阅读是,默认情况下 MySQL 无法计算密钥长度以强制执行唯一性约束;为什么 grails 不为此目的传递 maxSize 值?

更新:2012 年 6 月 8 日 这是我正在使用的休眠规范:

hibernate {
    cache.use_second_level_cache=true
    cache.use_query_cache=true
    cache.provider_class='net.sf.ehcache.hibernate.EhCacheProvider'
}

我正在使用带有mysql-connector-java-5.1.7-bin.jar 库的 com.mysql.jdbc.Driver 和 MySQL 5.5。

【问题讨论】:

  • 您在 DataSource.groovy 中指定了哪种 Hibernate 方言?
  • 我在问题中添加了休眠和 MySQL 信息
  • 我的意思是方言(应该是 MySQL5InnoDBDialect),它在 DataSource.groovy 的 dataSource { ... } 块中指定。

标签: mysql grails grails-orm


【解决方案1】:

您使用的是哪个版本的 mysql,varchar 列的最大长度可以是 255(在 mysql 5.0.3 之前) - 请参阅 http://dev.mysql.com/doc/refman/5.0/en/char.html

您可以尝试将列类型更改为text

【讨论】:

  • 我可以确认这个问题会影响 MySQL 5.5 版。使用 MySQL 5.5.28 和 Grails 2.1.1 时发生在我身上。
猜你喜欢
  • 1970-01-01
  • 2015-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-03
  • 2014-07-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多