Q: Grails Changing a Many-to-One from Set to List (Column needs to change) In a Grails domain object I am trying to change a domain object 
class Example {
    String name

    static hasMany = [objs:DomainObject]
}

to 
class Example {
    String name
    List objs

    static hasMany = [objs:DomainObject]
}

and I get this error.  
ERROR errors.GrailsExceptionResolver  - HibernateException occurred when processing request: [GET] /path/path
null index column for collection: com.XXXX. Stacktrace follows:
     org.hibernate.HibernateException: null index column for collection: com.XXXXXX
    at grails.converters.JSON.value(JSON.java:174)
    at grails.converters.JSON.convertAnother(JSON.java:162)
    at grails.converters.JSON.value(JSON.java:199)
    at grails.converters.JSON.convertAnother(JSON.java:162)
    at grails.converters.JSON.value(JSON.java:199)
    at grails.converters.JSON.convertAnother(JSON.java:162)
    at grails.converters.JSON.value(JSON.java:199)
    at grails.converters.JSON.convertAnother(JSON.java:162)
    at grails.converters.JSON.value(JSON.java:199)
    at grails.converters.JSON.render(JSON.java:134)
    at grails.converters.JSON.render(JSON.java:150)
    at com.XXXXController.Method(XXXXController.groovy:161)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:722)`

This happens on the production server.  I can't remove anything from the database, but I can fill in the empty column.  What do I need to do in the bootstrap that can fix or change the data in the current database?

A: Take a look at this article. It describes how to put random order in the new idx column.
http://tech.coterie.com/2012/11/migrating-from-set-to-list-in-grails.html
Here is the example SQL provided in the article:
insert into user_product(user_products_id, product_id, products_idx)
select t.user_id, t.product_id, (@rownum:=@rownum+1) % c.loves AS idx
from (
    select user_id, product_id
    from love_link
    order by user_id
) t,
(
    select user_id, count(*) as loves
    from love_link
    group by user_id
) c,
(SELECT @rownum:=0) r
where c.user_id=t.user_id
order by t.user_id;


A: Have a look at Lists of Objects. You need to change the way you are adding associations. Stress on the below example from the excerpts. 
Glimpse: 
// This won't work!
def book = new Book(title: 'The Shining')
book.save()
author.addToBooks(book)

// Do it this way instead.
def book = new Book(title: 'Misery')
author.addToBooks(book)
author.save()


A: This error happen when you save the instance as the next, lets suppose that we have a list of users in a chat, and we want to add a user to the chat (like a group):
class ChatUserService {

    def addUserToChat(User user, Chat chat) {
        ChatUser chatUser = new ChatUser()
        chatUser.user = user
        chatUser.chat = chat // set the parent in the child
        if (!chatUser.save()) {
            throw new SaveDomainException(chatUser)
        }
    }
}

This works fine if the you declare the list in the hasMany property.
In the other case, you need to add it to the parent, and save the parent to store the index as the next:
class ChatUserService {

    def addUserToChat(User user, Chat chat) {
        ChatUser chatUser = new ChatUser()
        chatUser.user = user
        chatUser.chat = chat
        chat.addToUsers(chatUser) // add the child to the parent
        if (!chat.save()) {
            throw new SaveDomainException(chatUser)
        }
    }
}


A: Taking MYSQL as example: say you've defined domains User and Car as one-to-many relation:
class User {
    String name;
    // List cars;
    static hasMany = [cars: Car]
}

Class Car {
    String regNum;
    static belongsTo = [user: User]
}

Before change relation to List, database table Car would be something like:
| Field      | Type         | Null | Key | Default | Extra |
| id                          | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| regNum                      | varchar(255) | YES  |     | NULL    |                |
As far as I know if you would change a many-to-one relation to List, Hibernate will create a car_idx column in the table.
| Field     | Type         | Null | Key | Default | Extra          |
...
| car_idx   | int(11)      | YES  |     | NULL    |                |
If there had been some data before changing, car_idx would be set to NULL by default, which is the root cause.
So you can run a script to set default value on idx to solve this problem.
