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-----数据库一对多实例

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