简体   繁体   中英

Scala Slick table inheritance

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!

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM