2016-12-05 78 views
0

我試圖開發一個地址簿的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):

+1

第二種方法(與許多模型)正是它是如何在關係數據庫(如MySQL) – CrowbarKZ

回答

0

,除非你使用相同的模型,並覆蓋其夫婦時間

如果把它拆分爲2種機型,例如我不認爲DRY是有關模型的設計: 用戶,users_details 所以它的邏輯分離權威性的數據和個人數據 但在這情況下,以多分離表

爲什麼你需要的電話,網站這麼多表格,電子郵件 也許你的數據設計ISN」糾正

+0

分離只是一個例子,因爲我不知道如何處理不確定數量的字段(例如, Phone1,Phone2,Phone3等)。 –

+0

,如果你將有數目不詳的某些類型的字段,所以使它不同的模型ForiegnKey –

+0

如果不需要通過這個數據,使查找和你行與您可以使用JSON數據存儲乘法項目等領域解壓 –