Josbel Luna
Josbel Luna

Reputation: 2814

GORM creating tables with base class name instead concrete class name

I Have the following domains in my gorm package:

Domain.groovy

package gorm

class Domain {
    String createdBy
    static constraints = {
    }
    static mapping = {
        tablePerHierarchy true
    }

}

User.groovy

package gorm

class User extends Domain {
    String name

    static constraints = {

    }
}

I want a table named user with the fields of the base class domain, but instead GROM generate a table whit this specification

create table domain
(
    id bigint auto_increment
        primary key,
    version bigint not null,
    created_by varchar(255) not null,
    class varchar(255) not null,
    name varchar(255) null
)

I'm using mysql driver with grails 2.5.6.

Upvotes: 0

Views: 684

Answers (1)

LeslieV
LeslieV

Reputation: 765

It generates the table with the name as domain because you are using tablePerHierarchy true in your mapping.

Basically, you will get one table where you can set different discriminators for the subclasses.

See here for more information on inheritance strategies: http://docs.grails.org/2.5.x/guide/single.html#GORM (scroll down to: 7.2.3 Inheritance in GORM)

If you simply want schema-export to generate your table with the name as user, then you would need to add the following to your mapping block in the Domain class:

table 'user'

so the entire mapping block would look like:

static mapping = {
    table 'user'
    tablePerHierarchy true
}

However, this may not make sense to name the table user if you have other classes extend from Domain. (and if you don't plan to have other classes extend from Domain, then just add your fields into your User domain).

If you want to generate two tables (Domain and User), then set tablePerHierachy false.

Here is a great write-up with examples that may help you decide which way you want to go for your project:

https://sysgears.com/articles/advanced-gorm-features-inheritance-embedded-data-maps-and-lists-storing/

As a side note: I'm not keen on the name Domain for a domain class; it is too generic and may get confusing when you are talking about the specific Domain class vs domain classes. At least name it BaseDomain.

Upvotes: 1

Related Questions