Scala Slick table inheritance

為{幸葍}努か 提交于 2019-12-09 13:07:32

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/19309183/scala-slick-table-inheritance

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!