问题
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
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/7301427/specifying-field-size-of-map-collection-in-grails-dom