import elixir from elixir import Field, ManyToOne, OneToMany, OneToOne, ColumnProperty, using_options, using_options_defaults from sqlalchemy import types as T from functools import partial elixir.metadata.bind = "sqlite:///test.sqlite" elixir.metadata.bind.echo = True ReqField = partial(Field, required = True) class Entity (elixir.Entity): using_options(abstract = True) using_options_defaults(shortnames = True) class Category (Entity): name = Field(T.String(50), unique = True) parent = ManyToOne('Category') children = OneToMany('Category') def __repr__ (self): if self.parent: return '' % (self.name, self.parent.name) else: return '' % self.name class Expense (Entity): using_options(abstract = True) description = Field(T.String(50)) expense = ReqField(T.Numeric(scale = 2)) category = ManyToOne('Category', required = True, innerjoin = True) class SingleExpense (Expense): date = ReqField(T.Date) class ConstExpense (Expense): months = ReqField(T.Integer) start = ReqField(T.Date) end = ReqField(T.Date) monthly = ColumnProperty(lambda c: c.expense / c.months) next = OneToOne('ConstExpense', inverse = 'prev') prev = ManyToOne('ConstExpense') elixir.setup_all() session = elixir.session if __name__ == "__main__": elixir.create_all()