Skip to content

Commit 3591f14

Browse files
committed
Model 생성 및 DB 적용
1 parent c376abb commit 3591f14

5 files changed

Lines changed: 112 additions & 3 deletions

File tree

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@
55
.vscode
66

77
# python
8-
__pycache__
8+
__pycache__
9+
10+
# database
11+
migrations

Pipfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ pylint = "*"
1111
[packages]
1212
flask = "*"
1313
flask-script = "*"
14+
flask-sqlalchemy = "*"
15+
flask-migrate = "*"
16+
pymysql = "*"
1417

1518
[requires]
1619
python_version = "3.7"

Pipfile.lock

Lines changed: 65 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/__init__.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,50 @@
33
"""
44

55
from flask import Flask
6+
from flask_sqlalchemy import SQLAlchemy
7+
from sqlalchemy.sql import text
8+
9+
DB = SQLAlchemy()
10+
SQLALCHEMY_DATABASE_URI = \
11+
("mysql+pymysql://{USER}:{PASSWORD}@{ADDR}:{PORT}/{NAME}?charset=utf8")
12+
13+
class Userinfo(DB.Model):
14+
""" Userinfo model """
15+
__tablename__ = "userinfo"
16+
__table_args__ = {'mysql_collate' : 'utf8_general_ci'}
17+
id = DB.Column("id", DB.Integer, primary_key=True)
18+
name = DB.Column("name", DB.String(250), nullable=False)
19+
tel = DB.Column("tel", DB.String(20), nullable=False)
20+
created = DB.Column(DB.TIMESTAMP, server_default=text("CURRENT_TIMESTAMP"), nullable=False)
21+
22+
def __init__(self, name, tel):
23+
self.name = name
24+
self.tel = tel
625

726
def create_app() -> (Flask):
827
""" create_app() 함수를 호출해 앱을 초기화 """
28+
29+
""" app config part """
30+
# 나중에 config는 다 빼야 할 것 같다.
931
app = Flask(__name__)
1032
app.app_context().push()
33+
app.config['SQLALCHEMY_DATABASE_URI'] = SQLALCHEMY_DATABASE_URI.format(
34+
USER="root",
35+
PASSWORD="1234",
36+
ADDR="127.0.0.1",
37+
PORT=3306,
38+
NAME="board"
39+
)
40+
app.config['SQLALCHEMY_ECHO'] = True
41+
app.config['DEBUG'] = True
42+
DB.init_app(app)
1143

44+
""" route part """
45+
# 나중에 route는 다 빼야 할 것 같다.
1246
@app.route("/")
1347
def index():
1448
""" / url index """
1549
return "<h1>Hello World ! <br> This is index page</h1>"
1650

51+
""" return part """
1752
return app

manage.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
"""
44

55
from flask_script import Manager
6+
from flask_migrate import Migrate, MigrateCommand
67
from app import create_app
8+
from app import DB
9+
from app import Userinfo
710

811
APP = create_app()
9-
1012
MANAGER = Manager(APP)
13+
MIGRATE = Migrate(APP, DB)
14+
MANAGER.add_command('db', MigrateCommand)
1115

1216
@MANAGER.command
1317
def run():

0 commit comments

Comments
 (0)