2

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.

Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121
user3240644
  • 2,211
  • 2
  • 24
  • 35
  • grails don't know about which dialect you will use that's why it didn't do that job and left it over to dialect to say whether some column name etc are valid as per db or not. – Vinay Prajapati Jan 07 '16 at 13:24
  • Though codenarc is just warning you about this and not forcing to remove this. You may use db migration but without changing your domain field names by using data column: 'some_other_name'. Even after this if codenarc says problem then codenarc needs some upgrades specific to grails :) – Vinay Prajapati Jan 07 '16 at 13:26

1 Answers1

3

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.

Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121