How can I create named unique constraint for multiple columns?
I've three classes:
class Descriptor {
// some columns
}
class Protein {
// some columns
}
class DescriptorValue {
// some columns
static belongsTo = [protein: Protein, descriptor: Descriptor]
static constraints = {
protein(unique:['descriptor'])
}
}
GORM creates an index with an auto generated name that is different for different environments. How can I specify its name?
Try to do it like:
static mapping = {
protein unique:['descriptor'], index: 'protein_idx' //or whatever name you like
}
If you need to use multi-column indices, then you can specify for every property the same index name.
String field1
String field2
Integer field3
SomeObject object
static constraints = {
object unique: ['field1','field2', 'field3']
}
Related
How can I get a list of field names for a domain class that are not nullable?
For instance, in the following domain:
class MyDomain {
String field1
String field2
String field3
String field4
static constraints = {
field2 nullable: true
field3 nullable: true
}
}
How can I get back the list ['field1','field4'] in a controller?
I'm validating rows in a CSV, and some of the row information is different from what is stored in the domain, so it would be preferable to get a List of String names rather than bind to a command object with exclusions.
You can use the constrainedProperties. It gives all the constraints of the particular domain class.
And now you want only the non-nullble constraints then filter out the result for it.
Example :
MyDomain.constrainedProperties.findResults { it.value.nullable ? null : it.key }
Output :
['field1','field4']
For grails 2.x users :
MyDomain.getConstraints().findResults { it.value.nullable ? null : it.key }
You need to use the PersistentEntity API
Set<String> propertyNames = [] as Set
for (PersistentProperty prop: MyDomain.gormPersistentEntity.persistentProperties) {
if (!prop.mapping.mappedForm.nullable) {
propertyNames.add(prop.name)
}
}
You may have to exclude things like version or timestamp properties depending on what you want.
I have written a domain class
class ReportCallByUser {
Integer userId;
String userName;
String reportName;
Date timeOfReportCall;
static constraints = {
}
static mapping = {
timeOfReportCall index: 'time_of_report_call_index'
}
The last line creates an index in database but in ascending order. How can I create index on 'timeOfReportCall' with descending order?
Thanks in advance.
There's no such thing as order in regards to index creation. It's the query's ability to specify the ordering:
ReportCallByUser.list( [ sort:'timeOfReportCall', order:'[desc|asc]' ] )
I am assuming that you want the results in descending order by default when you query the database using GORM. If that is the case, you can specify sort order in your mapping as such.
static mapping = {
sort timeOfReportCall:"desc"
}
Hope that helps.
I have a domain class that looks like the following:
class Foo {
static hasMany = [bar: String]
}
The problem is that this creates a join table with a column of VARCHAR(255) in MySQL, which much larger than I need it to be. In my example, bar is a Set not an indexed collection, so trying to use indexColumn does not work. joinTable does not have an attribute to change the column type/length. Is it possible to change to column size without changing the structure of the domain class?
This works (tested with grails 2.4):
class Foo {
static hasMany = [
bars:String
]
static mapping = {
bars joinTable: [column: 'BARS_STRING', length: 112]
}
}
You can also try sqlType, like
class Foo {
static hasMany = [bar: String]
static mapping = {
names joinTable: [column: 'bar', sqlType: 'varchar(32)']
}
}
My Alert has many Location objects, and I have the join table alert_locations.
The generated columns are:
alerts_locations_id (I want this to be alert_id)
location_id
Here's my domain object:
class Alerts {
static hasMany = [locations:Locations,notifications:Notifications]
Date alertDateTime
String pest
String crop
static constraints = {
alertDateTime (blank:false)
pest (blank:false)
crop (blank:false)
}
}
static mapping = {
locations joinTable:[column:"location_id", key:"alert_id"]
Suppose I have a class with following definition:
class ClassA {
static belongsTo = [ownerB:ClassB, ownerC:ClassC]
}
How I can enforce an unique constraint on above "belongsTo" relationship?
Using the Map form of belongsTo creates fields with those names, so you can do it with
static constraints = {
ownerB unique: 'ownerC'
}
Refer http://grails.1312388.n4.nabble.com/Creating-a-foreign-key-constraint-td1352053.html
String field1
String field2
Integer field3
SomeObject object
static constraints = {
object unique: ['field1','field2', 'field3']
}