2008-10-05 23:02:10 +02:00
|
|
|
import logging
|
2008-10-05 17:23:54 +02:00
|
|
|
import sqlalchemy as sa
|
|
|
|
from sqlalchemy import orm
|
|
|
|
|
|
|
|
from pyalchemybiz.model import meta
|
2008-10-05 23:02:10 +02:00
|
|
|
from pyalchemybiz.model import person, customer, product
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
2008-10-05 17:23:54 +02:00
|
|
|
|
|
|
|
def init_model(engine):
|
|
|
|
"""Call me before using any of the tables or classes in the model."""
|
|
|
|
|
|
|
|
sm = orm.sessionmaker(autoflush=True, transactional=True, bind=engine)
|
|
|
|
|
|
|
|
meta.engine = engine
|
|
|
|
meta.Session = orm.scoped_session(sm)
|
2008-10-05 23:02:10 +02:00
|
|
|
person.t_person = sa.Table(
|
|
|
|
'person', meta.metadata, autoload=True, autoload_with=engine)
|
|
|
|
customer.t_customer = sa.Table(
|
|
|
|
'customer', meta.metadata, autoload=True, autoload_with=engine)
|
|
|
|
product.t_producttype = sa.Table(
|
|
|
|
'producttype', meta.metadata, autoload=True, autoload_with=engine)
|
|
|
|
product.t_product = sa.Table(
|
|
|
|
'product', meta.metadata, autoload=True, autoload_with=engine)
|
2008-10-05 17:23:54 +02:00
|
|
|
|
2008-10-05 23:02:10 +02:00
|
|
|
orm.mapper(person.Person, person.t_person)
|
2008-10-05 17:23:54 +02:00
|
|
|
orm.mapper(customer.Customer, customer.t_customer)
|
2008-10-05 23:02:10 +02:00
|
|
|
customer.Customer.person = orm.relation(person.Person)
|
|
|
|
orm.mapper(product.ProductType, product.t_producttype)
|
|
|
|
orm.mapper(product.Product, product.t_product)
|
|
|
|
product.Product.producttype = orm.relation(
|
|
|
|
product.Product, backref=orm.backref('products', lazy='dynamic'))
|