How to model a contract database (with several buyers or sellers) using GAE datastore

ぃ、小莉子 提交于 2019-12-12 06:25:43

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/11294526/how-to-model-a-contract-database-with-several-buyers-or-sellers-using-gae-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!