In Postgres you can use tree data with LTREE.
http://patshaughnessy.net/2017/12/11/trying-to-represent-a-t...
http://patshaughnessy.net/2017/12/11/trying-to-represent-a-t...
@maxk42 approach with the relationship table works well for both read and write intensive applications.