-
Notifications
You must be signed in to change notification settings - Fork 2
/
sql_model.py
89 lines (65 loc) · 2.55 KB
/
sql_model.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import os
import json
from sqlalchemy import Column, String, Integer, Float, Table, ForeignKey
from sqlalchemy.orm import relationship, backref
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
SECRET_DIR = os.path.join(ROOT_DIR, '.secrets')
secrets = json.load(open(os.path.join(SECRET_DIR, "secrets.json")))
engine = create_engine('mysql+pymysql://{}:{}@{}:{}/{}'.format(
secrets['RDS_USER_ID'],
secrets['RDS_USER_PASSWORD'],
secrets['RDS_USER_URL'],
secrets['RDS_PORT'],
secrets['DATABASE_NAME']
))
Session = sessionmaker(bind=engine)
Base = declarative_base()
info_association = Table(
'total_information', Base.metadata,
Column('RegionID', Integer, ForeignKey('regions.id')),
Column('CategoryID', Integer, ForeignKey('facilities.id')),
Column('ObjectID', Integer, ForeignKey('informations.id')),
Column('category_id', Integer, ForeignKey('categories.id'))
)
class Region(Base): # 지역 구분
__tablename__ = 'regions'
id = Column(Integer, primary_key=True)
code = Column(String(32)) # 지구구분코드
name = Column(String(64))
def __init__(self, code, name):
self.code = code
self.name = name
class Category(Base):
__tablename__ = 'categories'
id = Column(Integer, primary_key=True)
name = Column(String(32))
def __init__(self, name):
self.name = name
class Facility(Base): # 종목 구분
__tablename__ = 'facilities'
id = Column(Integer, primary_key=True)
code = Column(String(32)) # 지형지물 코드
name = Column(String(32))
def __init__(self, code, name):
self.code = code
self.name = name
class Information(Base): # 종합 정보
__tablename__ = 'informations'
id = Column(Integer, primary_key=True)
region_id = Column(Integer, ForeignKey('regions.id'), nullable=False)
facility_id = Column(Integer, ForeignKey('facilities.id'), nullable=False)
category_id = Column(Integer, ForeignKey('categories.id'), nullable=False)
name = Column(String(64))
tel = Column(String(64))
fare = Column(String(256))
latitude = Column(Float)
longitude = Column(Float)
def __init__(self, name, tel, fare, latitude, longitude):
self.name = name
self.tel = tel
self.fare = fare
self.latitude = latitude
self.longitude = longitude