json-server的增刪改查 官網(wǎng)已經(jīng)十分詳盡,不多說
但是它沒告訴我級聯(lián)操作和主鍵相關(guān)的東東啊。。。也許后端人員清楚,但是后端誰用json-server啊。。。。我服
主鍵問題
拿官網(wǎng)的json來分析:
{
"posts": [
{ "id": 1, "title": "json-server", "author": "typicode" }
],
"comments": [
{ "id": 1, "body": "some comment", "postId": 1 }
],
"profile": { "name": "typicode" }
}
我們需要知道的是:
所有的 一級 都是表。 如 posts comments
表中的id就是主鍵,這是默認(rèn)的
把表的復(fù)數(shù)形式去掉,加上“Id”,在別的表中引用,就是外鍵
那么我們就知道了 posts和comments是如何關(guān)聯(lián)的了
級聯(lián)刪除問題
級聯(lián)刪除的定義為:
外鍵的級聯(lián)刪除:如果父表中的記錄被刪除,則子表中對應(yīng)的記錄自動被刪除
父表——被外鍵引用的表
子表——引用父表中的鍵作為外鍵的表
父表是posts
子表是comments
我來運(yùn)行一下 delete http://localhost:3000/posts/1
Paste_Image.png
其json文件變成了
{
"posts": [],
"comments": [],
"profile": {
"name": "typicode"
}
}
由此可見
json-server是可以做級聯(lián)刪除的
是否支持多級級聯(lián)刪除
我們的假數(shù)據(jù)如下:
{
"grandFathers": [
{
"id":1,
"name":"jack-grandFather"
},
{
"id":2,
"name":"tom-grandFather"
}
],
"fathers": [
{
"id":1,
"name":"jack-father",
"grandFatherId":1
},
{
"id":2,
"name":"tom-father",
"grandFatherId":2
}
],
"sons": [
{
"id":1,
"name":"jack",
"fatherId":1
},
{
"id":2,
"name":"tom",
"fatherId":2
}
]
}
然后我刪除一條grandFather
Paste_Image.png
最后的json為
{
"grandFathers": [
{
"id": 2,
"name": "tom-grandFather"
}
],
"fathers": [
{
"id": 2,
"name": "tom-father",
"grandFatherId": 2
}
],
"sons": [
{
"id": 1,
"name": "jack",
"fatherId": 1
},
{
"id": 2,
"name": "tom",
"fatherId": 2
}
]
}
由此可見
json-server不支持多級級聯(lián)刪除
搜索 標(biāo)簽
http://localhost:3000/posts?label=aaa