Exposed icon indicating copy to clipboard operation
Exposed copied to clipboard

Support entity inheritance via "extension" tables

Open Tapac opened this issue 7 years ago • 2 comments

Allow to implement inheritance in something like:

object BaseTable : IdTable()
object ExtendTable : IdTable() {
   val parent = reference(BaseTable)
}

class ExtendEntity : Entity {
   val field1 by BaseTable.field1
   val field2 by ExtendTable .field2
   val dependsOnTable = BaseTable innerJoin ExtendTable
...
}

Tapac avatar Feb 16 '18 10:02 Tapac

How ids are propagated from BaseTable to ExtendTable? Does it depend on the order of innerJoin arguments? What if I need several base tables (one for each interface/superclass)?

How this would work when another table references BaseTable? The code doesn't know whether it is ExtendEntity or Extend2Entity. In my experience, BaseTable has enum column specifying concrete type.

grv87 avatar Oct 14 '21 08:10 grv87

Going to bump an old issue, as this is one of the requirements I have in my environment. We have a base "Client" table that contains information common to all types of clients (Name, email, contact, etc), and we have three extension tables "Customer", "Account", "Subscription" that represent each level of our customer hierarchy (customers own accounts, accounts own subscriptions). We also have a similar structure with our payment tables, where a base payment table is extended into the different payment types (debit, credit, check, etc).

My thought as to how this might work (and this is just a brainstorming thing without looking at the code to see if it's feasible) is to add a new parameter to the constructor for Table that allows the specification of a "parent" Table. The "child" table will inherit all the columns from the parent table without having to redefine them. When converted to SQL, a parent/child relationship will be signified by an INNER JOIN between the two that is inserted by default anywhere the child table is used. If you reference just the parent, this join will be left out because the child isn't necessary and Exposed wouldn't necessarily have a way to know which child table to use. When it comes to Entities, same thing. The Entity built off the parent can reference only the parent's columns, but an Entity built off the child can reference either the parent or child columns.

Like I said, I was just brainstorming this, but does anyone have any other thoughts?

zackman0010 avatar Dec 02 '25 19:12 zackman0010