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']
}
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 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)']
}
}
I am receiving the following errors in my "Class" class in Grails. For each field, it is telling me that I have a duplicate field. Which doesn't make any sense, because all I'm trying to do is map the fields with their associated table columns. The class fields and the fields in my mapping closure are all underlined. Here is my class so far:
package booklist
class Class {
Integer id
String name
String description
String instructor
String courseNumber
String lineNumber
List books
BigDecimal bookTotalPrice
String sequenceNumber
String subjectCode
static constraints = {
}
static mapping = {
//Uses the default datasource
table ''
columns {
id column: 'class_id'
name column: 'class_name'
description column: 'course_description'
instructor column: 'instructor_name'
courseNumber column: 'course_number'
lineNumber column: 'line_number'
bookTotalPrice column: 'book_total_price'
sequenceNumber column: 'sequence_number'
subjectCode column: 'subject_code'
}
}
}
You don't need to declared in static mapping the fields that you don't need to rename. Just write this:
package booklist
class MyClass {
Integer id
String name
String description
String instructor
String courseNumber
String lineNumber
List books
BigDecimal bookTotalPrice
String sequenceNumber
String subjectCode
static mapping = {
description column: 'course_description'
}
}
Grails works with the CoC (Convention Over Configuration) approach: if you don't need to change something, don't write it and a convention will be used.
For more details about the column mapping, take a look at the Grails documentation: http://grails.org/doc/latest/ref/Database%20Mapping/column.html
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']
}
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"]