我試圖開發一個地址簿的MySQL架構。我的不確定性在於如何對它進行建模,因爲有些聯繫人會有一個電話號碼和一個電子郵件地址,而其他聯繫人可能有多個這樣的電話號碼。在前端,我將創建一個允許用戶添加地址字段的按鈕。數據庫設計通訊簿
現在我的模型看起來是這樣的:
class Person(models.Model):
sex = models.CharField(max_length=32)
first_names = models.CharField(max_length=64)
last_names = models.CharField(max_length=64)
date_of_birth = models.DateField()
marital_status = models.CharField(max_length=64)
nationality = models.CharField(max_length=64)
address_street = models.CharField(max_length=64)
address_alt_line = models.CharField(max_length=64)
address_zip = models.CharField(max_length=64)
address_state = models.CharField(max_length=64)
address_city = models.CharField(max_length=64)
address_country = models.CharField(max_length=64)
phone1 = models.CharField(max_length=64)
phone2 = models.CharField(max_length=64)
phone3 = models.CharField(max_length=64)
phone4 = models.CharField(max_length=64)
phone5 = models.CharField(max_length=64)
email1 = models.CharField(max_length=64)
email2 = models.CharField(max_length=64)
email3 = models.CharField(max_length=64)
email4 = models.CharField(max_length=64)
email5 = models.CharField(max_length=64)
website1 = models.CharField(max_length=64)
website2 = models.CharField(max_length=64)
website3 = models.CharField(max_length=64)
website4 = models.CharField(max_length=64)
website5 = models.CharField(max_length=64)
website6 = models.CharField(max_length=64)
website7 = models.CharField(max_length=64)
website8 = models.CharField(max_length=64)
website9 = models.CharField(max_length=64)
但是我知道,這不是很乾。我想到了另一種選擇是創建了地址,電子郵箱,電話號碼,網站等人士和獨立的那些獨立的模型,然後進行person.id這些模型外鍵:
class Person(models.Model):
sex = models.CharField(max_length=32)
first_names = models.CharField(max_length=64)
last_names = models.CharField(max_length=64)
date_of_birth = models.DateField()
marital_status = models.CharField(max_length=64)
nationality = models.CharField(max_length=64)
class Address(models.Model):
street = models.CharField(max_length=64)
alt_line = models.CharField(max_length=64)
zip = models.CharField(max_length=64)
state = models.CharField(max_length=64)
city = models.CharField(max_length=64)
country = models.CharField(max_length=64)
type = models.CharField(max_length=64)
owner = models.ForeignKey(Person, on_delete=models.CASCADE)
class Phone(models.Model):
number = models.CharField(max_length=64)
type = models.CharField(max_length=32)
owner = models.ForeignKey(Person, on_delete=models.CASCADE)
class OnlineContact(models.Model):
address = models.CharField(max_length=64)
type = models.CharField(max_length=32)
owner = models.ForeignKey(Person, on_delete=models.CASCADE)
是任何這些方法好(足夠)還是有更好的方法來做到這一點?我寧願不使用NOSQL數據庫並堅持使用MySQL。僅供參考,我使用Python和Django。
類Person(models.Model):
第二種方法(與許多模型)正是它是如何在關係數據庫(如MySQL) – CrowbarKZ