简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM