douningqiu4991 2014-12-06 03:11 采纳率: 0%
浏览 69

数据存储中的数据建模

I just started using Datastore, so I'm still not sure about few things.

I have the fallowing entities:

Property: {ID, number, name, long, lat}

Address: {name, postCodeType}

City: {name}

Country: {name}

User: {name, username}

So the logic behind this is that a User will have multiple properties, which means that a property will hold a user key.

As described above the Property has some properties, but I am not sure on how to associate the address city and country.

I think a solution will be to store the keys for those 3 entities in the property entity.

type Property struct {   
  ID         int64   `json:"id" datastore:"-"`
  Number     int8    `json:"number"`
  Name       string  `json:"name"`
  Long       float64 `json:"long"`
  Lat        float64 `json:"lat"`
  AddressKey *datastore.Key
  CityKey    *datastore.Key
  CountryKey *datastore.Key
  UserKey    *datastore.Key
  CreatedAt  time.Time
}

Is my attempt from above a good start or what do I need to do different.

  • 写回答

1条回答 默认 最新

  • dongzhunqiu4841 2014-12-06 03:39
    关注
    1. A list of countries rarely changes, so most programmers use an enum (or a Goland equivalent) to represent countries, instead of creating entities in the datastore. You can use ether 2-letter or 3-letter country codes, and simply save a country code as a string property within an Address entity. The enum can also return a full display name of a country based on its code.

    2. I also don't see a reason to create an entity for cities, although it can be done, if really necessary. Usually, a city is saved as a string property, indexed if necessary, within an Address entity.

    This means that your Address entity may look like:

    Address: {name, street, city, country, postCode}
    
    1. Going further, if each property has only one address, you may not need the Address entity at all. Which leads us to a very simple solution:

      type Property struct {   
        ID         int64   `json:"id" datastore:"-"`
        Number     int8    `json:"number"`
        Name       string  `json:"name"`
        Long       float64 `json:"long"`
        Lat        float64 `json:"lat"`
        Street     string  `json:"street"`
        City       string  `json:"city"`
        Country    string  `json:"country"`
        PostCode   string  `json:"postCode"`
        UserKey    *datastore.Key
        CreatedAt  time.Time
      }
      
    评论

报告相同问题?

悬赏问题

  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序