Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala Slick table inheritance

Tags:

scala

slick

I have SQL table inheritance implemented this way:

Table Shape:
   Column   |  Type  
------------+---------
 shape_id   | integer
 square     | foat 
 name       | character varying(64)

Table Triangle
   Column   |  Type
------------+---------
 shape_id   | integer
 a          | float
 b          | float
 c          | float
Foreign-key constraints:
    "fkey1" FOREIGN KEY (shape_id) REFERENCES Shape(shape_id)

Table Circle
   Column   |  Type
------------+---------
 shape_id   | integer
 r          | float
Foreign-key constraints:
    "fkey2" FOREIGN KEY (shape_id) REFERENCES Shape(shape_id)

Is it possible with slick to create class model where Triangle extends Shape and Circle extends Shape?

I saw this question, but I don't like approach where all derived table columns put in one table as nullable.

Thank you!

like image 276
mtomy Avatar asked Oct 21 '22 23:10

mtomy


1 Answers

Slick is a relational/functional library. It does not map inheritance by itself. We talk about how to do Inheritance with Slick in our Scala Days 2013 talk. We describe how you can do Single Table Inheritance just like in the post you referenced. For Class Table Inheritance (which seems to be what you want) we suggest to model it in Slick using relationships instead. Instead of thinking "a circle is a shape" you would think "circle is a role a shape can take on". You will have to guarantee certain constraints yourself. For example that a shape is not at the same time a circle and a triangle. You can hide the mapping logic and the constraint validation behind an api, which you can add to your DAO. As you leave Slick's relational mode, you loose composability of queries (which you don't have in JPA, etc. anyways). We talk about this in Suggested Slick App Architecture and Composable / Re-usable queries.

like image 73
cvogt Avatar answered Nov 15 '22 06:11

cvogt