Flask-----数据库一对多实例

作者: 幼姿沫 | 来源:发表于2020-09-07 09:25 被阅读0次

多用户加外键    

role_id=db.Column(db.Integer,db.ForeignKey('roles.id'))

一方角色加关系映射

users=db.relationship('Users',backref='roles')

from flaskimport Flask

from flask_scriptimport Manager

from flask_sqlalchemyimport SQLAlchemy

from flask_migrateimport Migrate,MigrateCommand

app=Flask(__name__)

manager=Manager(app)

db=SQLAlchemy(app)

migrate=Migrate(app,db)

manager.add_command('db',MigrateCommand)

class Config(object):

DEBUG=True

    SQLALCHEMY_DATABASE_URI='mysql://root:root@localhost:3306/flask01'

    SQLALCHEMY_TRACK_MODIFICATIONS=True

    SQLALCHEMY_COMMIT_ON_TEAR=True

    SQLALCHEMY_ECHO=True

app.config.from_object(Config)

class Roles(db.Model):

__tablename__='roles'

    id=db.Column(db.Integer,autoincrement=True,primary_key=True)

name=db.Column(db.String(20),nullable=False)

info=db.Column(db.String(50))

users=db.relationship('Users',backref='roles')

def __init__(self,name,info):

self.name=name

        self.info=info

    def __str__(self):

return "name *  %s  info * %s" %self.name,self.info

class Users(db.Model):

__tablename__='usrs'

    id=db.Column(db.Integer,autoincrement=True,primary_key=True)

name=db.Column(db.String(50),nullable=False)

pwd=db.Column(db.String(20),nullable=False)

role_id=db.Column(db.Integer,db.ForeignKey('roles.id'))

def __init__(self,name,pwd,role_id):

self.name=name

        self.pwd=pwd

        self.role_id=role_id

@app.route('/')

def  index():

return '

使用manager扩展命令来迁移数据库

'

if __name__ =='__main__':

manager.run()


控制台显示

(first_flask) D:\Flask项目设计\review_first>python apps.py db init

D:\python\first_flask\lib\site-packages\flask_sqlalchemy\__init__.py:812: UserWarning: Neither SQ

LALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set. Defaulting SQLALCHEMY_DATABASE_URI to "sqlite:

///:memory:".

  warnings.warn(

D:\python\first_flask\lib\site-packages\flask_sqlalchemy\__init__.py:833: FSADeprecationWarning:

SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the f

uture.  Set it to True or False to suppress this warning.

  warnings.warn(FSADeprecationWarning(

Creating directory D:\Flask项目设计\review_first\migrations ...  done

Creating directory D:\Flask项目设计\review_first\migrations\versions ...  done

Generating D:\Flask项目设计\review_first\migrations\alembic.ini ...  done

Generating D:\Flask项目设计\review_first\migrations\env.py ...  done

Generating D:\Flask项目设计\review_first\migrations\README ...  done

Generating D:\Flask项目设计\review_first\migrations\script.py.mako ...  done

Please edit configuration/connection/logging settings in 'D:\\Flask项目设计\\review_first\\migrat

ions\\alembic.ini' before proceeding.

(first_flask) D:\Flask项目设计\review_first>python apps.py db migrate

D:\python\first_flask\lib\site-packages\flask_sqlalchemy\__init__.py:812: UserWarning: Neither SQ

LALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set. Defaulting SQLALCHEMY_DATABASE_URI to "sqlite:

///:memory:".

  warnings.warn(

D:\python\first_flask\lib\site-packages\flask_sqlalchemy\__init__.py:833: FSADeprecationWarning:

SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the f

uture.  Set it to True or False to suppress this warning.

  warnings.warn(FSADeprecationWarning(

INFO  [alembic.runtime.migration] Context impl MySQLImpl.

INFO  [alembic.runtime.migration] Will assume non-transactional DDL.

INFO  [alembic.autogenerate.compare] Detected added table 'roles'

Generating D:\Flask项目设计\review_first\migrations\versions\2ce58beca98f_.py ...  done

(first_flask) D:\Flask项目设计\review_first>python apps.py db upgrade

D:\python\first_flask\lib\site-packages\flask_sqlalchemy\__init__.py:812: UserWarning: Neither SQ

LALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set. Defaulting SQLALCHEMY_DATABASE_URI to "sqlite:

///:memory:".

(first_flask) D:\Flask项目设计\review_first>python apps.py db migrate

D:\python\first_flask\lib\site-packages\flask_sqlalchemy\__init__.py:812: UserWarning: Neither SQ

LALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set. Defaulting SQLALCHEMY_DATABASE_URI to "sqlite:

///:memory:".

  warnings.warn(

D:\python\first_flask\lib\site-packages\flask_sqlalchemy\__init__.py:833: FSADeprecationWarning:

SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the f

uture.  Set it to True or False to suppress this warning.

  warnings.warn(FSADeprecationWarning(

INFO  [alembic.runtime.migration] Context impl MySQLImpl.

INFO  [alembic.runtime.migration] Will assume non-transactional DDL.

INFO  [alembic.autogenerate.compare] Detected added column 'roles.info'

Generating D:\Flask项目设计\review_first\migrations\versions\51108d4647a0_.py ...  done

(first_flask) D:\Flask项目设计\review_first>python apps.py db upgrade

D:\python\first_flask\lib\site-packages\flask_sqlalchemy\__init__.py:812: UserWarning: Neither SQ

LALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set. Defaulting SQLALCHEMY_DATABASE_URI to "sqlite:

///:memory:".

  warnings.warn(

D:\python\first_flask\lib\site-packages\flask_sqlalchemy\__init__.py:833: FSADeprecationWarning:

SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the f

uture.  Set it to True or False to suppress this warning.

  warnings.warn(FSADeprecationWarning(

INFO  [alembic.runtime.migration] Context impl MySQLImpl.

(first_flask) D:\Flask项目设计\review_first>python apps.py db migrate

D:\python\first_flask\lib\site-packages\flask_sqlalchemy\__init__.py:812: UserWarning: Neither SQ

LALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set. Defaulting SQLALCHEMY_DATABASE_URI to "sqlite:

///:memory:".

  warnings.warn(

D:\python\first_flask\lib\site-packages\flask_sqlalchemy\__init__.py:833: FSADeprecationWarning:

SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the f

uture.  Set it to True or False to suppress this warning.

  warnings.warn(FSADeprecationWarning(

INFO  [alembic.runtime.migration] Context impl MySQLImpl.

INFO  [alembic.runtime.migration] Will assume non-transactional DDL.

INFO  [alembic.autogenerate.compare] Detected added table 'usrs'

Generating D:\Flask项目设计\review_first\migrations\versions\f042b5b9b41c_.py ...  done

(first_flask) D:\Flask项目设计\review_first>python apps.py db upgrade

D:\python\first_flask\lib\site-packages\flask_sqlalchemy\__init__.py:812: UserWarning: Neither SQL

SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the f

uture.  Set it to True or False to suppress this warning.

  warnings.warn(FSADeprecationWarning(

INFO  [alembic.runtime.migration] Context impl MySQLImpl.

INFO  [alembic.runtime.migration] Will assume non-transactional DDL.

INFO  [alembic.runtime.migration] Running upgrade 51108d4647a0 -> f042b5b9b41c, empty message

相关文章

  • Flask-----数据库一对多实例

    多用户加外键 role_id=db.Column(db.Integer,db.ForeignKey('roles....

  • 数据库一对一、一对多、多对多设计

    数据库一对一、一对多、多对多设计 数据库实体间有三种对应关系:一对一、一对多、多对多 一对一关系示例: 一个学生对...

  • 12 MySQL 多实例

    MySQL 多实例 [TOC] 多实例概述 什么是多实例: 在一台物理主机上运行多个数据库服务 优点: 节约运维成...

  • 数据库多实例

    数据库的基本操作都是基于单实例的,mysql多实例在实际生产环境也是非常实用的。 多实例主要作用是:充分...

  • redis系列题目总结

    1. 关系型与非关系型数据库对比? 关系型数据库是依据关系模型(一对一、一对多、多对多)来创建的数据库。 优点:...

  • python编写数据库表关系

    表关系(一对一,一对多,多对多) 查看数据库中已完成的表

  • MySQL一对一:一对多:多对多: 实例

    [MySQL一对一:一对多:多对多: 实例!!!!] 学生表和课程表可以多对多 一个学生可以学多门课程 一门课程可...

  • 原mysql基础上配置多实例

    原来通过yum安装了mysql数据库,在此基础上有添加一个实例,实现多实例,配置 多实例主要有两种方式,一个是每个...

  • 数据库多实例搭建

    一、环境规划 1.端口 : 3307,3308,3309 2.数据存储路径 : /data/330{7..9}/d...

  • mysql基础

    数据库常见的关系:一对一,一对多。多对多。 基础语法: 常见类型 数值类型 字符类型 日期类型 Blob类型 &&...

网友评论

    本文标题:Flask-----数据库一对多实例

    本文链接:https://www.haomeiwen.com/subject/wqrkektx.html