Installing the Flask-sqlalchemy, Pymysql module
Pip Install Flask-sqlalchemy Pymysql
Installing the MySQL Database
From Flask.ext.sqlalchemy import sqlalchemyfrom flask import Flask "metabase" app = Flask (__name__) app.config[' secret_ KEY '] = ' hard to guess ' # here is the root user, to fill in their own password, mysql default port is 3306, fill in the previously created database name Jianshu, connection method Reference # http://docs.sqlalchemy.org /en/latest/dialects/mysql.htmlapp.config[' Sqlalchemy_database_uri ']= ' mysql+pymysql://jianshu:[email protected] : 3306/jianshu ' #设置这一项是每次请求结束后都会自动提交数据库中的变动app. config[' Sqlalchemy_commit_on_teardown ']=true# instancing db = SQLALCHEMY (app )
Model definition
"Define the model, establish the relationship" class Role (db. Model): # defines the table name __tablename__ = ' roles ' # defines the Column object ID = db. Column (db. Integer, primary_key=true) name = db. Column (db. String (unique=true), user = db.relationship (' user ', backref= ' role ') #repr () method displays a readable string, although not entirely necessary, But it's good for debugging and testing. def __repr__ (self): return ' <role {}> '. Format (Self.name) class User (db. Model): __tablename__ = ' users ' id = db. Column (db. Integer, primary_key=true) username = db. Column (db. String (+), Unique=true, index=true) role_id = db. Column (db. Integer, Db. ForeignKey (' Roles.id ') def __repr__ (self): return ' <user {}> '. Format (self.username)
Relationship
A relational database establishes a connection in a different table by using a relationship. A diagram expresses a simple relationship between a user and a user role. This role and the user are a one-to-many relationship, because a role can belong to more than one user, and a user can only have a single role.
The following model classes show a one-to-many relationship expressed in.
Class Role (db. Model): # ... Users = db.relationship (' user ', backref= ' role ') class User (db. Model): # ... role_id = db. Column (db. Integer, Db. ForeignKey (' roles.id '))
#!/usr/bin/env pythonfrom exts Import dbfrom datetime import Datetimeclass User (db. Model): __tablename__ = ' user ' id = db. Column (db. Integer,primary_key=true,autoincrement=true) Username = db. Column (db. String (#), nullable=false) telephone = db. Column (db. String (one), nullable=false) password = db. Column (db. String (+), Nullable=false) class Questions (db). Model): __tablename__ = ' questions ' id = db. Column (db. Integer,primary_key=true,autoincrement=true) title = db. Column (db. String (+), nullable=false) content = db. Column (db. Text,nullable=false) Create_time = db. Column (db. Datetime,default=datetime.now) author_id = db. Column (db. Integer,db. ForeignKey (' user.id ')) Author = db.relationship (' user ', Backref=db.backref (' Questions ')) class Answer (db. Model): __tablename__ = ' answer ' ID = db. Column (db. integer,primary_key=true,autoincrement=true) content = db. Column (db. Text,nullable=false) question_id = db. Column (db. Integer,db. ForeignKey (' questions.id ')) author_id = db. Column (db. Integer,db. ForeignKey (' user.id ')) question = Db.relationship (' Questions ', backref = Db.backref (' answers ')) Author = db.relations Hip (' User ', backref = Db.backref (' answers '))
Flask + pymysql Operation MySQL Database