如何在 Elixir 中制作代码/查找表?

发布于 2024-10-27 22:22:27 字数 252 浏览 5 评论 0原文

我正在寻找制作查找表或使用由 Python 的 Elixir 制作的关系数据库中的代码的最佳方法。我什至不确定我这里的术语是否正确。

例如,我有一个包含区域列的位置表。我希望“区域”列的值仅包含“北美”、“中美洲”、“南美洲”、“亚洲/太平洋岛屿”和其他几个值。值列表将来可能会发生变化。

Elixir 如何做到这一点?使用枚举似乎是一个坏主意,因为这些值是长文本字符串。似乎某种代码会更好(例如1 =北美,2 =南美等)如何在数据库中存储和引用这些代码?

I am looking for a/the best way to make lookup tables or use codes in a relational database made from Python's Elixir. I am not even sure my terminology here is correct.

For example, I have a Location table that has a Region column. I want the values for the Region column to only have values of "North America", "Central America", "South America", "Asia/Pacific Islands", and several others. The list of values might change in the future.

How can this be done with Elixir? Using an Enum seems like a bad idea because the values are long text strings. It seems that some kind of code would be better (like 1=North America, 2=South America, etc.) How do I store and reference these codes in the database?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弄潮 2024-11-03 22:22:27

一个建议是标准化您的数据,即在您的位置表中,区域列是一个整数值,代表您的一个区域。然后创建一个区域表,仅列出您的区域名称一次。因此,位置表仅引用区域表的索引(或外键)。

例如:您的“区域”表如下所示:

  • id=1,regionname=北美
  • id=2,regionname=南美洲
  • id=3,regionname=中美洲
  • id=4,regionname=亚洲/太平洋群岛

然后,您的“位置”表只是索引:

  • id=1,region=1
  • id=2,region=2
  • =3,region=3
  • id
  • id=4,region=4 id=5,region=2
  • id=6,region=1

这是一个简单的,如果粗略的话,例如:

from elixir import *

metadata.bind = "sqlite:///"

class Regions(Entity):    
    regionname = Field(String(255))

class Location(Entity):    
    region = ManyToOne('Regions')

setup_all()
create_all()

#Create the region names:
na_temp = Regions(regionname="North America")
sa_temp = Regions(regionname="South America")
ca_temp = Regions(regionname="Central America")
ap_temp = Regions(regionname="Asia/Pacific Islands")
session.commit()

#Create links to each region in the location table:
northamerica = Location(region=na_temp)
southamerica = Location(region=sa_temp)
centamerica = Location(region=ca_temp)
asiapacific = Location(region=ap_temp)
anotherarea = Location(region=sa_temp)
yetanotherarea = Location(region=na_temp)
session.commit()

#Get all items from the Location table:
locations = Location.query.all()

#Display the contents of the Location table, and lookup the name from the Regions table
for place in locations:
    print "Location table id: {}".format(place.region_id)    
    print "Lookup region name: {}".format(Regions.get_by(id=place.region_id).regionname)
    print

有不止一种方法可以做到这一点,这只是我的方法;我不是你会遇到的最强大的 Python 程序员。

One suggestion would be to normalize your data, i.e., in your Location table, the Region column is an Integer value, representing one of your Regions. Then create a Regions table that list your region name only once. Thus the Location table just references an index (or Foreign Key) to your Regions table.

For example: your Regions table is something like this:

  • id=1, regionname=North America
  • id=2, regionname=South America
  • id=3, regionname=Central America
  • id=4, regionname=Asia/Pacific Islands

Then, your Locations table just indexes this:

  • id=1, region=1
  • id=2, region=2
  • id=3, region=3
  • id=4, region=4
  • id=5, region=2
  • id=6, region=1

Here is a simple, if crude, example:

from elixir import *

metadata.bind = "sqlite:///"

class Regions(Entity):    
    regionname = Field(String(255))

class Location(Entity):    
    region = ManyToOne('Regions')

setup_all()
create_all()

#Create the region names:
na_temp = Regions(regionname="North America")
sa_temp = Regions(regionname="South America")
ca_temp = Regions(regionname="Central America")
ap_temp = Regions(regionname="Asia/Pacific Islands")
session.commit()

#Create links to each region in the location table:
northamerica = Location(region=na_temp)
southamerica = Location(region=sa_temp)
centamerica = Location(region=ca_temp)
asiapacific = Location(region=ap_temp)
anotherarea = Location(region=sa_temp)
yetanotherarea = Location(region=na_temp)
session.commit()

#Get all items from the Location table:
locations = Location.query.all()

#Display the contents of the Location table, and lookup the name from the Regions table
for place in locations:
    print "Location table id: {}".format(place.region_id)    
    print "Lookup region name: {}".format(Regions.get_by(id=place.region_id).regionname)
    print

There is more than on way to do this, this is just my approach; I'm not the strongest Python programmer you'll meet.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文