【问题标题】:How to model a contract database (with several buyers or sellers) using GAE datastore如何使用 GAE 数据存储对合同数据库(具有多个买家或卖家)进行建模
【发布时间】:2012-07-02 13:18:30
【问题描述】:

我是编程新手,我正在尝试掌握 GAE 数据存储的概念。我正在尝试构建一个应用程序以简化编写合同(http://contractpy.appspot.com),我想知道:如何为数据库建模以记录合同(考虑到合同可以具有 交易的同一方有几个人)?

在关系模型中,我会执行以下操作:为人员创建一个表,然后为合同创建一个表,然后为参与该合同的人员创建第三个表。看起来像这样(我猜):

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 数据存储非关系概念的正确思维方式。

我在 Python 2.7 中使用 GAE。

提前感谢任何帮助/建议!

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"]))

【问题讨论】:

    标签: python google-app-engine google-cloud-datastore non-relational-database


    【解决方案1】:

    只是前面的一些想法。不要在您的课程中使用复数形式。当你获取一个实体时,它不是一个“人”,而是一个人或一方。您也有错误的参考属性。作为起点,您可以使用以下内容。

    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。实体 Key 足以唯一标识合同。
    2. 以合同为父级创建缔约方实体。那么与合同关联的所有 ContractingParty 实体都将是子实体,并且在同一个实体组中。
    3. 请注意个人属性 ContractingParty 中的 collection_name。这意味着给定一个人员对象,您可以获取所有引用该人员的 ContractingParty 记录。 <someperson>.party_to_contracts.run() 。然后,您可以通过调用 parent() 从 ContractingParty 实体获取合同。您仍然可以在不定义collection_name 的情况下执行此操作,但在这种情况下,它将被称为contractingparty_set。
    4. 给定一份​​合同,您可以使用ContractingParty.all().ancestor(thecontract).run() 获取所有各方

    如果不完全了解您的应用程序,我不能直接推荐更精细的模型,但这会起作用,并且取决于您在这里尝试做的事情。

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-26
      • 1970-01-01
      • 2023-03-21
      • 2018-03-02
      • 2013-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多