在数据存储中的数据建模

huangapple go评论74阅读模式
英文:

Data modeling in datastore

问题

我刚开始使用Datastore,所以对一些事情还不太确定。

我有以下实体:

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

Address: {name, postCodeType}

City: {name}

Country: {name}

User: {name, username}

根据上述描述,User将拥有多个属性,这意味着一个属性将保存一个用户键

根据上述描述,Property有一些属性,但我不确定如何关联addresscitycountry

我认为一个解决方案是在property实体中存储这3个实体的键。

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
}

我上面的尝试是否是一个好的开始,还是我需要做一些不同的事情?

英文:

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

得分: 3

  1. 国家列表很少改变,所以大多数程序员使用枚举(或类似Goland的东西)来表示国家,而不是在数据存储中创建实体。您可以使用两个字母或三个字母的国家代码,并将国家代码简单地保存为地址实体中的字符串属性。枚举还可以根据代码返回国家的完整显示名称。

  2. 我也不认为有必要为城市创建实体,尽管如果确实需要的话可以这样做。通常,城市会作为字符串属性保存在地址实体中,如果需要的话进行索引。

这意味着您的地址实体可能如下所示:

Address: {name, street, city, country, postCode}
  1. 更进一步,如果每个属性只有一个地址,您可能根本不需要地址实体。这将导致一个非常简单的解决方案:

    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
    }

英文:
  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
     }
    

huangapple
  • 本文由 发表于 2014年12月6日 11:11:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/27327899.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定