apache / apache/grails-core

Mapping problem with tablePerSubclass if parent is abstract class

Open
#14,643 0 comments 0 reactions 0 assignees View on GitHub
relates-to: gorm
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

# Problem

Table Per subclass not working if parent is abstract class
# Scenario

I have created following hierarchy of domain classes

Grails 3.0.10
JVM 1.7

abstract class Image(){
String name
String path
mapping:{
tablePerHierarchy false
id column: "image_id"
}
}

class userImage extends Image(){
User user
static belongsTo:[User]
mapping:{
id column: "image_id"
}
}

class itemImage extends Image(){
Item item
static belongsTo:[Item]
mapping:{
id column: "image_id"
}
}

class User(){
String name
static hasMany:[images:UserImage]
}

class Item(){
String name
static hasMany:[images:ItemImage]
}

Bootstrap.groovy

Item itm = new Item()
itm.name = "item name"

ItemImage img = new ItemImage()
img.name = "img name"
img.path = "dummy path"

itm.addToImages(img)

itm.save(flush:true)

// successfully saved the record in 3 tables
item
item_mage
image

def newItem = item.get(itm.id) // pass newly created id
def images = item.images

ERROR : column image_0.class not found.
#

If i try to define images as type of Image it gives problem at insert, as follows
- It restricts me to define itemImages id key as column "item_image_id"
- It try to insert into item_image twice
- When I look in to table I found there are two foreign keys created one as image and another for images

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the hierarchy in Grails 3.0.10 with an abstract Image parent and table-per-subclass mappings. Inspect the generated item, item_image, and image tables and the query for item.images, then compare the behavior when associations use Image versus ItemImage. Done means loading the association succeeds without the missing image_0.class column or duplicate inserts and foreign keys.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy
Domain
backend, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.