如何使用GAE数据存储模拟合同数据库(与多个买家或卖家) [英] How to model a contract database (with several buyers or sellers) using GAE datastore

查看:121
本文介绍了如何使用GAE数据存储模拟合同数据库(与多个买家或卖家)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

pre codeREATE TABLE people
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合约(
idContract INTEGER(7)NOT NULL AUTO_INCREMENT PRIMARY KEY,
contractType VACHAR(12),#购买合约,租赁合约等
contractDate DATE,
放置VACHAR(12),
)ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci;

CREATE TABLE contractingParties(
FK_id INTEGER(7)NOT NULL FOREIGN KEY(FK_id)引用people(id),
FK_idContract INTEGER(7)NOT NULL FOREIGN KEY(FK_idContract)引用契约(idContract),
条件VACHAR(12),#例如,买方,卖方,承租人,所有者等
)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.InteProProperty(required = True)
address = db.PostalAdressProperty(required = True)

class Contracts(db.Model):
idContract = db.InteProProperty(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([买方,卖方,租客,所有者,证人]))


解决方案

前面有一些想法。不要在你的课堂上使用复数形式。当你获取一个实体时,它不是一个人,而是一个人或一方。你也有错误的方式你的参考性质。作为一个起点,你可以使用下面的代码。

  class Person(db.Model):
name = db。 StringProperty(required = True)
profession = db.StringProperty(required = True)
driverLicense = db.InteProProperty(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([买方,卖方,租客,所有者,证人]))

有些事情要 注意。


  1. 除非您需要可设置的(外部来源的)合同编号,否则合约实体中不需要idContract。实体Key将足以唯一地标识合同。
  2. 创建合同作为父项的ContractingParty实体。然后,与合同关联的所有ContractingParty实体都将是子项,并位于同一个实体组中。

  3. 请注意person属性ContractingParty中的collection_name。这意味着,给定一个人对象,然后可以获取引用该人的所有ContractingParty记录。 < someperson> .party_to_contracts.run()。然后,您可以通过调用 parent()来从ContractingParty实体获取合同。您仍然可以在不定义collection_name的情况下执行此操作,但在这种情况下,它将被称为contractingparty_set。

  4. 给定合约,您可以使用 ContractingParty.all()。ancestor(thecontract).run()

如果没有完全理解你的应用程序,我不能直接推荐一个更精确的模型,但这是可行的,并且基于你所拥有的试图在这里做。



希望它有帮助。


I'm new to programming and I'm trying to grasp the concept of the GAE datastore. I'm trying to build an app to make it easy write contracts (http://contractpy.appspot.com) and I'd like to know: how to model a database to record contracts (considering that a contract can have several people on the same side of the transaction)?

In the relational model, I would do the following: create a table for people, then a table for the contract and a third table with reference to people that have participated in that contract. Would look something like this (I guess):

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;

My question is: what is the best way to do this in GAE datastore? I did a sketch below, but I'm not sure that this is the correct way of thinking using the GAE datastore nonrelational concept.

I'm using GAE with Python 2.7.

Thanks in advance for any help/advice!

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

解决方案

Just some thoughts up front. Don't use plurals for your classes. When you fetching an entity it is not a "People" but a Person or Party. You also have your reference properties around the wrong way. As a starting point you could use the following.

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

Some things to note.

  1. You don't need idContract in the Contract entity unless you need a settable (externally sourced) contract number. The entities Key will be enough to uniquely identify the contract.
  2. Create the ContractingParty entities with the Contract as a parent. Then all ContractingParty entities associated with the contract will be children, and in the same entity group.
  3. Note the collection_name in the person property ContractingParty. This means that given a person object you can then get all ContractingParty records that refer to the person. <someperson>.party_to_contracts.run() . Then you can get the contract from the ContractingParty entity by calling parent() on it. You can still do this without defining collection_name, but in this case it would be called contractingparty_set.
  4. Given a Contract you can fetch all parties with ContractingParty.all().ancestor(thecontract).run()

Without a full understanding of you application I can't directly recommend a more refined model, but this would work and is based on your what you have tried to do here.

Hope it helps.

这篇关于如何使用GAE数据存储模拟合同数据库(与多个买家或卖家)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆