From 3fd6c511d8674615c5f91656ce52032b25e6cbc6 Mon Sep 17 00:00:00 2001 From: RealMadrid <15989835@qq.com> Date: Mon, 1 May 2017 22:58:01 +1000 Subject: [PATCH] try db_migrate --- app.db | Bin 6144 -> 7168 bytes app/__init__.py | 14 ++++++- app/forms.py | 3 +- app/models.py | 16 +++++++- db_repository/versions/002_migration.py | 28 ++++++++++++++ tmp/add_user.py | 49 ++++++++++++++++++++++++ 6 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 db_repository/versions/002_migration.py create mode 100644 tmp/add_user.py diff --git a/app.db b/app.db index 2a6300d673db2175ece1e3730afe92cf8691bfc7..39810c0c0844464bdc2c58e3956a098a3e67bf34 100644 GIT binary patch delta 467 zcmZoLXt0s%Z#Q>EX=2vPi+=t*~jc8#l*rOsq0gk zoSB!J1123(a}zUj^pf*)fnJpo*A7n3FDWSo5eOMBP$;l4FfhMi{th(f0kZ-Zvm_(L z6ege)Cs-3))eYviKvid$jYODr8PRP4sTW2wpBE^`$h?z*c_&ccdS*6PMi%3A#>oeS z9T=GwiLh{i))MHO$IPml-cJP1r2R63e*x zGNUQeW?_z-%o8WbvvGm+v1}IPxQ&I&#S4^VWZuQVybGvx12dZoBa3l5=j8pu4vR!s E0AGq59{>OV diff --git a/app/__init__.py b/app/__init__.py index f558b5f..8521a8a 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,8 +1,18 @@ from flask import Flask -from flask.ext.sqlalchemy import SQLAlchemy +from flask_sqlalchemy import SQLAlchemy +from app import views, models + +import os +from flask.ext.login import LoginManager +from flask.ext.openid import OpenID +from config import basedir +# from flask.ext.sqlalchemy import SQLAlchemy app = Flask(__name__) app.config.from_object('config') db = SQLAlchemy(app) -from app import views, models \ No newline at end of file + +lm = LoginManager() +lm.init_app(app) +oid = OpenID(app, os.path.join(basedir, 'tmp')) diff --git a/app/forms.py b/app/forms.py index 3ea2d7e..6142970 100644 --- a/app/forms.py +++ b/app/forms.py @@ -1,4 +1,5 @@ -from flask.ext.wtf import Form +from flask_wtf import Form +# from flask.ext.wtf import Form from wtforms import StringField, BooleanField from wtforms.validators import DataRequired diff --git a/app/models.py b/app/models.py index a2690ac..f196dcf 100644 --- a/app/models.py +++ b/app/models.py @@ -1,9 +1,23 @@ from app import db + class User(db.Model): + # 对于一个一对多的关系,db.relationship 字段通常是定义在“一”这一边 + # 在这种关系下,得到一个 user.posts 成员,它给出一个用户所有的 blog id = db.Column(db.Integer, primary_key = True) nickname = db.Column(db.String(64), index = True, unique = True) email = db.Column(db.String(120), index = True, unique = True) + post = db.relationship('Post', backref='author', lazy='dynamic') + + def __repr__(self): + return '' % self.nickname + + +class Post(db.Model): + id = db.Column(db.Integer, primary_key=True) + body = db.Column(db.String(140)) + timestamp = db.Column(db.DateTime) + user_id = db.Column(db.Integer, db.ForeignKey('user.id')) def __repr__(self): - return '' % (self.nickname) \ No newline at end of file + return '' % self.body \ No newline at end of file diff --git a/db_repository/versions/002_migration.py b/db_repository/versions/002_migration.py new file mode 100644 index 0000000..192d0f1 --- /dev/null +++ b/db_repository/versions/002_migration.py @@ -0,0 +1,28 @@ +from sqlalchemy import * +from migrate import * + + +from migrate.changeset import schema +pre_meta = MetaData() +post_meta = MetaData() +post = Table('post', post_meta, + Column('id', Integer, primary_key=True, nullable=False), + Column('body', String(length=140)), + Column('timestamp', DateTime), + Column('user_id', Integer), +) + + +def upgrade(migrate_engine): + # Upgrade operations go here. Don't create your own engine; bind + # migrate_engine to your metadata + pre_meta.bind = migrate_engine + post_meta.bind = migrate_engine + post_meta.tables['post'].create() + + +def downgrade(migrate_engine): + # Operations to reverse the above upgrade go here. + pre_meta.bind = migrate_engine + post_meta.bind = migrate_engine + post_meta.tables['post'].drop() diff --git a/tmp/add_user.py b/tmp/add_user.py new file mode 100644 index 0000000..b2d1781 --- /dev/null +++ b/tmp/add_user.py @@ -0,0 +1,49 @@ +from app import db, models +''' +u = models.User(nickname='Scott', email='scott@email.com') +db.session.add(u) +db.session.commit() + +u = models.User(nickname='Lucine', email='lucine@email.com') +db.session.add(u) +db.session.commit() + +users = models.User.query.all() +print(users) + +u = models.User.query.get(1) +print(u) + +import datetime +u = models.User.query.get(1) +p = models.Post(body='my first post!', timestamp=datetime.datetime.utcnow(), author=u) +db.session.add(p) +db.session.commit() + +# get all posts from a user +u = models.User.query.get(1) +print(u) +posts = u.posts.all() +print(posts) + +# obtain author of each post +for p in posts: + print(p.id,p.author.nickname,p.body) + +# a user that has no posts +u = models.User.query.get(2) +print(u) +print(u.posts.all()) + +# get all users in reverse alphabetical order +print(models.User.query.order_by('nickname desc').all()) +''' + + +users = models.User.query.all() +for u in users: + db.session.delete(u) +posts = models.Post.query.all() +for p in posts: + db.session.delete(p) +db.session.commit()