Wednesday, May 21, 2008

What to do when Grails does not generate table for your domain class

Situation I hit was that while running on HSQL file based database everything was running perfectly. Then I have decided to change database to MySQL. Well, in grails this is really easy. Just add MySql jdbc driver, change DataSource.groovy file, manually create empty database and start application. You see no errors in console so you access you application through browser and here comes surprise. You get stacktrace claiming that table is missing. You check the database and table is really missing. How is it possible? There is no error reported?

After some googling and reading this post I found solution or better solution how to find solution :)

Go to Config.groovy and log4j hibernate level change to the debug. Start the application and hibernate will display all actions it performs. In the big stacktrace you should be able to find description what happend. In my case it was:

[1704] hbm2ddl.SchemaUpdate Unsuccessful: create table tutorial_link (id bigint ...
[1704] hbm2ddl.SchemaUpdate BLOB/TEXT column 'link' used in key specification ...

So it turned out its not a problem of Grails, nor problem of Hibernate but different handling of columns by different databases.

However it took me some time to find out and understand what is happening so I decided to post it so maybe I will save somebody's time.

5 comments:

Graeme Rocher said...

The solution here would be to use back ticks to escape the name of the column:

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

diongillard said...

I raised a JIRA for this issue.
Grails really shouldn't startup if the tables aren't all there....

http://jira.codehaus.org/browse/GRAILS-2898

jan said...

Actually in my case I didn't have a problem with reserved MySql keywords. My problem was that column link was defined as: maxSize:700, unique:true and Hibernate tries to generate this column as of unique Text type and that fails.

I couldn't find solution within grails how to specify that String property longer than 255 and shorter than 760 chars be created as varchar column in MySql.

board tc said...

To see the hibernate output from the docs:

make the change
log4j.logger.org.hibernate="debug, stdout"

in config.groovy. The default is "off".

Anonymous said...

I had a similar problem, but as for me the problem was because in the domain class I declared a varchar that exceds the maximun size permited in DB2, then the only thing that I done was to use the maxSize constraint and it works.