0

我是新手編程,我試圖掌握GAE數據存儲的概念。我正在嘗試構建一個應用程序來簡化寫合同(http://contractpy.appspot.com),並且我想知道:如何建模數據庫以記錄合同(考慮合同可以有幾個幾個交易的同一方)?如何使用GAE數據存儲建模合同數據庫(與多個買家或賣家)

在關係模型中,我將執行以下操作:爲人員創建表格,然後爲合同創建表格,併爲參與合同的人員創建第三個表格。會是這個樣子(我猜):

CREATE TABLE people(
id INTEGER(8) NOT NULL AUTO_INCREMENT PRIMARY KEY, 
name VARCHAR(250) NOT NULL,  
profession VARCHAR(30), 
driverLicense VARCHAR(12), 
address VACHAR(60) 
)ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci; 

CREATE TABLE contracts(
idContract INTEGER(7) NOT NULL AUTO_INCREMENT PRIMARY KEY, 
contractType VACHAR(12), # purchase contract, rental contract etc. 
contractDate DATE, 
place VACHAR(12), 
)ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci; 

CREATE TABLE contractingParties(
FK_id INTEGER(7) NOT NULL FOREIGN KEY(FK_id) references people(id), 
FK_idContract INTEGER(7) NOT NULL FOREIGN KEY(FK_idContract) references contracts(idContract),  
condition VACHAR(12), # e.g., buyer, seller, renter, owner etc. 
)ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci; 

我的問題是:什麼是GAE數據存儲做到這一點的最好方法是什麼?我在下面做了一個草圖,但我不確定這是使用GAE數據存儲非關係概念的正確思路。

我使用GAE與Python 2.7。

在此先感謝您的任何幫助/建議!

class People(db.Model): 
    name = db.StringProperty(required = True) 
    profession = db.StringProperty(required = True) 
    driverLicense = db.IntegerProperty(required = True) 
    address = db.PostalAdressProperty(required = True) 

class Contracts(db.Model): 
    idContract = db.IntegerProperty(required = True) 
    contractType = db.StringProperty(required = True, choices=set(["Purchase Agreement", "Rental House", "Rental Car"])) 
    contractDate = db.DateProperty (required = True, auto_now = True, auto_now_add = True) 
    place = db.StringProperty(required = True) 
    parties = db.ReferenceProperty(ContractingParties, required = True) 

class ContractingParties(db.Model): 
    person = db.ReferenceProperty(People, required=True) 
    contract = db.ReferenceProperty(Contracts, required=True) 
    condition = db.StringProperty(required = False, choices=set(["buyer", "seller", "renter", "owner", "witness"])) 

回答

2

只是想到了一些想法。不要在你的課堂上使用複數形式。當你獲取一個實體時,它不是一個「人」,而是一個人或一方。你也有錯誤的方式你的參考性質。作爲一個起點,你可以使用以下內容。

class Person(db.Model): 
    name = db.StringProperty(required = True) 
    profession = db.StringProperty(required = True) 
    driverLicense = db.IntegerProperty(required = True) 
    address = db.PostalAdressProperty(required = True) 

class Contract(db.Model): 
    idContract = db.IntegerProperty(required = True) 
    contractType = db.StringProperty(required = True, choices=set(["Purchase Agreement",  "Rental House", "Rental Car"])) 
    contractDate = db.DateProperty (required = True, auto_now = True, auto_now_add = True) 
    place = db.StringProperty(required = True) 

class ContractingParty(db.Model): 
    person = db.ReferenceProperty(People, required=True, collection_name="party_to_contracts") 
    condition = db.StringProperty(required = False, choices=set(["buyer", "seller", "renter", "owner", "witness"])) 

有些事情要注意。

  1. 除非需要可設置的(外部來源的)合同編號,否則合同實體中不需要idContract。實體密鑰足以唯一標識合同。
  2. 創建合同作爲父項的ContractingParty實體。然後,與合同相關聯的所有ContractingParty實體都將是子項,並且位於同一個實體組中。
  3. 請注意人物業ContractingParty中的collection_name。這意味着,給定一個人對象,然後可以獲取引用該人的所有ContractingParty記錄。 <someperson>.party_to_contracts.run()。然後,您可以通過呼叫parent()來從ContractingParty實體獲得合同。您仍然可以在不定義collection_name的情況下執行此操作,但在這種情況下,它將被稱爲contractingparty_set。
  4. 給定一個合同,你可以用ContractingParty.all().ancestor(thecontract).run()

取各方沒有你的應用有充分的瞭解,我不能直接推薦一個更精緻的模型,但是這會工作,並根據你的,你已經嘗試了什麼在這裏做。

希望它有幫助。

+0

謝謝@Tim霍夫曼,你的建議幫了我很多! – craftApprentice