在 grails DOM 中指定 Map 集合的字段大小
我有一个 Grails 1.3.7 域类,其声明如下:
class Document {
String url
Map metadata = new HashMap()
static constraints = {
url(nullable:false, blank: false, maxSize: 2048)
metadata()
}
}
生成的模式如下所示:
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| url | longtext | NO | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
+--------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| metadata | bigint(20) | YES | | NULL | |
| metadata_idx | varchar(255) | YES | | NULL | |
| metadata_elt | varchar(255) | NO | | NULL | |
+--------------+--------------+------+-----+---------+-------+
我想知道如何为 document_metadata 表指定不同的类型(具体来说,不同的大小)。我希望能够存储长度超过 255 个字符的字符串。我在网上找不到任何相关文档,可能是因为我想不出任何好的关键字。地图和集合是非常通用的术语!
谢谢,
吉恩
I have a Grails 1.3.7 domain class that is declared like this:
class Document {
String url
Map metadata = new HashMap()
static constraints = {
url(nullable:false, blank: false, maxSize: 2048)
metadata()
}
}
The schema that is generated looks like this:
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| url | longtext | NO | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
+--------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| metadata | bigint(20) | YES | | NULL | |
| metadata_idx | varchar(255) | YES | | NULL | |
| metadata_elt | varchar(255) | NO | | NULL | |
+--------------+--------------+------+-----+---------+-------+
I am wondering how I can specify different types (specifically, different size) for the document_metadata table. I would like to be able to store strings longer than 255 characters. I could not find any relevant documentation online, possibly because I couldn't come up with any good keywords. Map and Collection are pretty generic terms!
Thanks,
Gene
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
能够影响“元数据”的 DDL 的唯一方法是使其成为具体的域类。使用 HashMap 并不会让你有能力这样做。
The only way you are going to be able to influence the DDL for your "metadata" is to make it a concrete domain class. Using HashMap isn't going to give you the ability to do so.
只需对数据库运行 sql alter 命令就非常容易了。这个 alter 命令的一个好地方是 BootStrap,并带有一些检查表列定义是否正常的逻辑。 Grails 不会触及已经创建的列。
It would be quite easy just to run an sql alter command to database. A good place for this alter command would be BootStrap and with some logic that checks the table column definitions wheter they're ok or not. Grails won't touch already created columns.