DynamoDB 在一张表中处理一对多对多

时间:2021-04-13 16:10:44

标签: amazon-dynamodb

我一直在为我一直在读的一张桌子概念而苦苦挣扎,并想知道我是否走上了正确的道路。

假设有一个场景,您希望有一个用户(那个)拥有许多车辆政策(第一个),并且每个政策(现在是一个)映射到该政策涵盖的许多车辆(另外许多) .所以,一对多对多。像这样:

enter image description here

在 DynamoDB 中,您处理分区键和排序键,而不是外键,也没有连接。

我读到的是,在这种情况下处理一对多关系的一种方法是去规范化并使用映射。

到目前为止我想出的东西是这样的:

{
 "ModelName": "User Policies",
 "ModelMetadata": {
   "Author": "John Doe",
   "DateCreated": "Apr 12, 2021, 03:06 PM",
   "DateLastModified": "Apr 13, 2021, 11:01 AM",
   "Description": "",
   "AWSService": "Amazon DynamoDB",
   "Version": "2.0"
 },
 "DataModel": [
 {
  "TableName": "user_vehicles",
  "KeyAttributes": {
    "PartitionKey": {
      "AttributeName": "user_id",
      "AttributeType": "S"
    },
    "SortKey": {
      "AttributeName": "policy_id",
      "AttributeType": "S"
    }
  },
  "NonKeyAttributes": [
    {
      "AttributeName": "sv_latitude",
      "AttributeType": "S"
    },
    {
      "AttributeName": "sv_longitude",
      "AttributeType": "S"
    },
    {
      "AttributeName": "status",
      "AttributeType": "S"
    },
    {
      "AttributeName": "vehicles",
      "AttributeType": "M"
    }
  ],
  "DataAccess": {
    "MySql": {}
   }
  }
 ]
}

然后对于属性车辆,它将如下所示:

{
"V01": {
    "id": "sw10302"
    "make": "Ford",
    "model": "Mustang",
    "year": "1966",
    "color": "Black",
    "vin": "1NJ378H81934C2932"
},
"V02": {
    "id": "sw10615"
    "make": "Chevrolet",
    "model": "Corvette",
    "year": "1959",
    "color": "Red",
    "vin": "2CH378481634G2937"
 }
}

这看起来有道理吗?

0 个答案:

没有答案
相关问题