我在Quota上問過(How do I implement a MySQL inheritance relations with a Node.js database ORM (like sequelizejs)?),再這里重發(fā)一下,求有經(jīng)驗的各位指導(dǎo)下:
大體是這樣的,最近在寫個項目,做了下簡單的表設(shè)計,其中的部分如下:
(其中三個表共享id,id即是外鍵)之所以想這么弄的原因是,這個項目的內(nèi)容主體是project
(不在上圖中),而一個項目可以屬于一個app
也可以屬于user
,為了統(tǒng)一,我希望每個項目都有一個唯一全局的名稱namespace/project_name
,為了這個目的,我把app
和user
抽象出一個父類,也就是owner
(或者直接換成namespace也可以),而且這樣后續(xù)我通過這個唯一的項目名稱查詢的時候也不需要預(yù)先做判斷這個namespace
是應(yīng)用還是用戶。
但是在常見的這些 ORM 庫中沒有找到支持這么做的API,但是我又覺得這樣得設(shè)計應(yīng)該不是很少見,所以想知道大家一般是怎么處理的。
光陰似箭催人老,日月如移越少年。
Actually, you think this is an "inheritance" relationship, but it is essentially a "one-to-one one-way mapping", so just use Sequelize's hasOne to establish the relationship:
App.hasOne Owner
User.hasOne Owner
app_or_user.getOwner().then (owner) ->
console.log owner.name
In addition, shouldn’t I go to StackOverflow? Why go to Quota?