-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase_setup.py
52 lines (44 loc) · 1.53 KB
/
database_setup.py
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
41
42
43
44
45
46
47
48
49
50
51
52
import sys
from sqlalchemy import create_engine, Column, ForeignKey, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
Base = declarative_base()
class Store(Base):
__tablename__ = 'stores'
id = Column(Integer, primary_key=True)
store_name = Column(String(250), nullable=False)
store_img_url = Column(String(250), nullable=False)
store_description = Column(String(250), nullable=False)
store_owner_name = Column(String(250), nullable=False)
@property
def to_json(self):
return {
"id": self.id,
"storeName": self.store_name,
"storeImgURL": self.store_img_url,
"storeDescription": self.store_description,
"storeOwnerName": self.store_owner_name
}
class Product(Base):
__tablename__ = 'products'
id = Column(Integer, primary_key=True)
product_name = Column(String(250), nullable=False)
product_img_url = Column(String(250), nullable=False)
product_description = Column(String(250), nullable=False)
product_price = Column(Integer, nullable=False)
product_category = Column(String(250), nullable=False)
store_id = Column(Integer, ForeignKey('stores.id'))
stores = relationship(Store)
@property
def to_json(self):
return {
"id": self.id,
"productName": self.product_name,
"productImgURL": self.product_img_url,
"productDescription": self.product_description,
"productPrice": self.product_price,
"productCategory": self.product_category,
"storeID": self.store_id
}
engine = create_engine('sqlite:///stores.db')
Base.metadata.create_all(engine)