Scala Slick 表继承 [英] Scala Slick table inheritance

查看:48
本文介绍了Scala Slick 表继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以这种方式实现了 SQL 表继承:

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)

是否可以使用 slick 创建类模型,其中三角形扩展形状和圆形扩展形状?

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.

谢谢!

推荐答案

Slick 是一个关系/功能库.它本身不映射继承.我们在 2013 年 Scala Days 演讲中讨论了如何继承 Slick.我们描述了如何执行单表继承,就像在您引用的帖子中一样.对于 Class Table Inheritance(这似乎是您想要的),我们建议将其建模使用关系代替.您不会认为圆形是一种形状",而是会认为圆形是一种形状可以承担的角色".您必须自己保证某些限制.例如,形状不能同时是圆形和三角形.您可以将映射逻辑和约束验证隐藏在 api 后面,您可以将其添加到您的 DAO 中.当您离开 Slick 的关系模式时,您会失去查询的可组合性(无论如何您在 JPA 等中都没有).我们在建议的流畅应用架构可组合/可重用查询.

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.

这篇关于Scala Slick 表继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆