user3240644
user3240644

Reputation: 2281

Grails Domain - Reserved Keyword Names

So I've finally introduced codenarc into my project's build process and I'm getting the following warning - GrailsDomainReservedSqlKeywordName.

The problem is that I have quite a few of these warnings, namely because I've named quite a few domain fields as data. For example

class FormData {

  Long data
  static constraints = {
    data nullable: true
  }
  ...

The effect of this according to codenarc is

Naming a domain class (or its field) with such a keyword causes SQL schema creation errors and/or redundant table/column name mappings.

My question is: Should I now rename all my data properties and if so, how best to do it (perhaps using database migration)?

I'm just wondering why the Grails documentation did not warn against using these reserved keywords. Perhaps they should.

Upvotes: 2

Views: 1214

Answers (1)

Shashank Agrawal
Shashank Agrawal

Reputation: 25797

There is a way to prevent this with Grails. Add a mapping in your FormData domain class like this:

class FormData {

    Long data

    static constraints = {
        data nullable: true
    }

    static mapping = {
        data column: '`data`'
    }
}

Grails/Hibernate allows you to use backticks (`) to allowing the name to be escaped and now you don't have to write any database migration. You can simply be using your data field as is.

Upvotes: 3

Related Questions