db = DAL('sqlite://acacrits.db')

db.define_table(
    'athlete',
    Field('last_name', required=True),
    Field('first_name', required=True),
    Field('additional_names'),
    Field('suffix'),
    Field('gender', length=1, required=True, requires=IS_IN_SET(('m', 'f'))),
    Field('usac_license_numer', 'integer'))
db.athlete.id.readable = False

db.define_table(
    'official',
    Field('last_name', required=True),
    Field('first_name', required=True),
    Field('additional_names'),
    Field('suffix'))
db.official.id.readable = False

db.define_table(
    'marshal',
    Field('last_name', required=True),
    Field('first_name', required=True),
    Field('additional_names'),
    Field('suffix'))
db.marshal.id.readable = False

db.define_table(
    'season',
    Field('year', 'integer', required=True, unique=True))

db.define_table(
    'race_class',
    Field('description', required=True, unique=True))

db.define_table(
    'race_subclass',
    Field('description', required=True, unique=True))

db.define_table(
    'race_subclass_season',
    Field('race_subclass_id', 'reference race_subclass'),
    Field('race_class_season_id', 'reference race_class_season'))

db.define_table(
    'raceday',
    Field('date', 'date', required=True),
    Field('conditions'),
    Field('temperature_f', 'double'))

db.define_table(
    'racedayofficial',
    Field('raceday_id', 'reference raceday'),
    Field('official_id', 'reference official'))

db.define_table(
    'race',
    Field('date', 'date', required=True),
    Field('class_id', 'reference race_class', required=True,
          requires=IS_IN_DB(db, db.race_class.id, '%(description)s')),
    Field('laps', 'integer'),
    Field('slow_lap_secs', 'double'),
    Field('fast_lap_secs', 'double'),
    Field('average_lap_secs', 'double'))

db.define_table(
    'team',
    Field('name', required=True, unique=True))
db.team.id.readable = False

db.define_table(
    'athleteteam',
    Field('athlete_id', 'reference athlete'),
    Field('team_id', 'reference team'),
    Field('season_id', 'reference season',
          requires=IS_IN_DB(db, db.season.id, '%(year)d')))

db.define_table(
    'participant',
    Field('athlete_id', 'reference athlete', required=True),
    Field('team_id', 'reference team'),
    Field('race_id', 'reference race', required=True))

db.define_table(
    'result',
    Field('participant_id', 'reference participant', required=True),
    Field('place', 'integer'),
    Field('points', 'double'),
    Field('mar_place', 'integer'),
    Field('point_prime', 'boolean'))

db.define_table(
    'subclassresult',
    Field('participant_id', 'reference participant', required=True),
    Field('subclass_id', 'reference race_subclass', required=True),
    Field('place', 'integer'),
    Field('points', 'double'))

db.define_table(
    'prime',
    Field('participant_id', 'reference participant'),
    Field('description'))


# Hard-coded tables.
def _():
    def update_table(table, table_data):
        cur_table_data = db().select(table.ALL)
        cur_keys = set()
        for row in cur_table_data:
            cur_keys.add(row.id)
            non_id_vals = {k: v for k, v in row.items() if k != 'id'}
            if row.id in table_data:
                table[row.id] = table_data[row.id]
            else:
                del table[row.id]
        for key, val in table_data.items():
            if not key in cur_keys:
                table.insert(**val)

    CURRENT_SEASON = 2015
    SEASONS_TABLE = {
        i+1: dict(year=year) for i, year in
        enumerate(xrange(1996, CURRENT_SEASON+1))}
    update_table(db.season, SEASONS_TABLE)

    CLASSES = ['A', 'B', 'C', 'Masters 40+/Women', 'Juniors']
    CLASSES_TABLE = {
        i+1: dict(description=description)
        for i, description in enumerate(CLASSES)}
    update_table(db.race_class, CLASSES_TABLE)

    db.commit()

_()

@@ -1013,178 +1013,8 @@
 ))%0A%0A
-db.define_table(%0A    'race_subclass_season',%0A    Field('race_subclass_id', 'reference race_subclass'),%0A    Field('race_class_season_id', 'reference race_class_season'))%0A%0A
 db.d
@@ -3690,24 +3690,236 @@
 SES_TABLE)%0A%0A
+    SUBCLASSES = %5B'Women', 'Juniors'%5D%0A    SUBCLASSES_TABLE = %7B%0A        i+1:dict(description=description)%0A        for i, description in enumerate(SUBCLASSES)%7D%0A    update_table(db.race_subclass, SUBCLASSES_TABLE)%0A%0A
     db.commi

Add some hard-coded subclasses to the data model.