summaryrefslogtreecommitdiff
path: root/db.py
blob: 5e13924248d96837f5f2f28c94c28d76d176d5e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import sqlalchemy
from sqlalchemy import *
from sqlalchemy.orm import sessionmaker

def setupdb():
	#engine = create_engine('sqlite:///Users/erdgeist/Coding/tinybill/foo.db', echo=True)
	engine = create_engine('sqlite:///foo.db', echo=True)
	metadata = MetaData()
	
	Table( 'customers', metadata,
		Column( 'id', Integer, primary_key=True ),
		Column( 'name', String, unique=True ),
		Column( 'email', String )
	)
	
	Table( 'products', metadata,
		Column( 'id', Integer, primary_key=True ),
		Column( 'type', String ),
		Column( 'name', String, unique=True ),
		Column( 'price', Integer ),
	)
	
	Table( 'purchases', metadata,
		Column( 'id', Integer, primary_key=True ),
		Column( 'product', Integer, ForeignKey('products.id' ) ),
		Column( 'customer', Integer, ForeignKey('customers.id' ) ),
		Column( 'detail', String ),
		Column( 'date', Date )
	)
	
	Table( 'payment', metadata,
		Column( 'id', Integer, primary_key=True ),
		Column( 'purchase', Integer, ForeignKey('purchases.id') ),
		Column( 'amount', Integer ),
		Column( 'date', Date )
	)
	
	metadata.create_all(engine)
	global Session
	Session = sessionmaker(bind=engine)