tags:

views:

234

answers:

2

I tried following this reference and this is now my domain's code:

class SnbrActVector {

    long nid
    String term
    double weight

    static mapping = {
        version false
        nid index:'Nid_Idx'
    }

    static constraints = {
        term(blank:false)
    }
}

What I want is to do is to add an index key for 'nid' column. I dropped the existing table and run the app again so the table is then recreated. However, when i check for list of indices, I can't see a 'Nid_Idx', only available is 'PRIMARY'. Do I have to manually create the index and name it 'Nid_idx in my mysql database?

Thanks.

A: 

You might need to add the column name to get it to fire e.g.

static mapping = {
        version false
        nid column:'nid', index:'Nid_Idx'
    }
leebutts
tested it and still not adding the index key automatically. pretty weird. btw, i'm using grails 1.2.1.
firnnauriel
+1  A: 

Your syntax is correct so it might be another problem or a Grails bug. My advice :

  1. Create a new grails application (grails create-app) with SnbrActVector as the only domain (grails create-domain...). Copy your code inside.
  2. Check that your DataSource.groovy file has dbCreate = "create-drop"
  3. Verify the indices of the SnbrActVector table.

If you see an index created for nid column, then it means that the problemn is in your application (you might not have used "create-drop" or something else)

If the index is NOT created => This is a grails bug and you should open a JIRA issue here

fabien7474
thanks for this suggestion. it seems that there's a bug in grails 1.2.1 when using dbCreate = "update". it is not properly creating the index keys. it only works when using "create-drop", which is not recommended in prod env. note that after the index key was created by 'create-drop' and you change it back to 'update', the index key will be gone again.
firnnauriel
It is not a bug.I don't think that dbCreate = "update" should add/remove database indexes of existing columns (could be too dangerous in production mdoe). You can raise an issue on JIRA or ask for it in grails forum (please post the link here).Anyway, you have your answer. In development mode, you don't need to manually add index (using dbCreate = "create") but in production mode, if the database already exists, you have to do it manually
fabien7474