背景
我们在做数据库设计的时侯一般需要画ER图方便数据库分析。 有很多ER设计工具,一般都是物理外键关系生成。
但是真实生产环境,我们很少会建立这种真实物理外键关系,仅仅只会在逻辑上生成一个虚拟的外键的关系。
具体原因可以参考阿里巴巴『Java 开发手册』

IDEA 针对上边的问题,在IDEA 2019.3.2 新增了虚拟外键的功能,在 ER 图增加生成虚拟外键的功能。
不过如果你的表中外键全名规则不符合规范,IDEA 是不会生成外键的。需要在 Setting|Editor|Code Competion设置。

上面功能比较适合表结构比较规范的表,但是真实场景可能比较复杂,外键命名也不是规范。为了生成这个外键关系,还要配置正则规则,好难啊,靠,放弃!!!!
有没有傻子也会操作的办法呀?
我们发现一个傻子都会的好办法,亲测可用!还是IDEA哟
首先我们需要打开一个 IDEA Database console 窗口,在里面输入连表 sql,然后使用 Alt+Enter 快捷键选择 Store table relation。

在工程根目录下会生成如下配置文件
sql
select * from comment
join reply_comment rc
on
comment.id = rc.root_reply_comment_id;
xml
<?xml version="1.0" encoding="UTF-8"?>
<data>
<object-set>
<table path="sparrow.reply_comment"> //select from comment
<table path="sparrow.reply_comment" //join reply_comment rc
from="id" to="reply_comment_id"/> //on comment.id =rc.root_reply_comment_id;
</table>
</object-set>
</data>
如果再添加其他虚拟外键,按以上规则生成即可!
xml
<?xml version="1.0" encoding="UTF-8"?>
<data>
<object-set>
<table path="sparrow.user_behavior">
<table path="sparrow.user" from="user_id" to="user_id"/>
</table>
<table path="sparrow.comment">
<table path="sparrow.user" from="create_user_id" to="user_id"/>
</table>
<table path="sparrow.comment">
<table path="sparrow.reply_comment" from="id" to="root_reply_comment_id"/>
</table>
<table path="sparrow.reply_comment">
<table path="sparrow.reply_comment" from="id" to="reply_comment_id"/>
</table>
<table path="sparrow.reply_comment">
<table path="sparrow.user" from="create_user_id" to="user_id"/>
</table>
<table path="sparrow.article">
<table path="sparrow.tag" from="tags" to="id"/>
</table>
</object-set>
</data>
最后选择Show Diagram 生成ER图

最终结果如图
