Skip to content
zzolo edited this page Aug 17, 2011 · 10 revisions

Models (database objects, schemas, mapping) are sadly in flux. But the flux is towards using SQLAlchemy.

New Modeling

New modeling is powered by SQLAlchemy, which has lots of great documentation.

Object Mapping

Currently object mapping is defined in giveaminute/models.py.

Transitional Object Mapping

In order to map to old models that are already defined in the database, the following sort of definition will create the object mapping:

class Project (Base):
    __tablename__ = 'project'
    __table_args__ = {'autoload': True}

Install Schema

See installation (Mac or Ubuntu) for details. But overall, run the following to get the schema installed:

python giveaminute/models.py

Querying

Something like the following (needs better information):

session = Controller.get_session()
query = session.query(ModelName)

Old Modeling

Schema

Schemas are actual SQL table definitions found in sql/models.sql. (There are some updates in sql/migrations/)

Updating Schema

To create a new models.sql, use a command similar to:

mysqldump --no-data -u USER_NAME -p DB_NAME | sed 's/\(.*ENGINE.*AUTO_INCREMENT=\).*/\10;/g' > sql/models-temp.sql

Make sure to create migration files in sql/migrations/ and update CHANGLOG.txt and UPGRADE.txt.

Object Modeling

Object modeling happens in multiple files in the giveaminute/ directory. There is a file for each major object.

Querying

Querying is done with web.py's database methods. For instance:

sql = 'select * from user where user_id = $userId'
data = db.query(sql, { 'userId': some_id })

The db object is created in the base controller (framework/controller.py) which is passed around, or retrieved with Controller.get_db().

Clone this wiki locally