首页 > 其他 > 详细

【FLASK】使用ORM以及类似于Django更新新增字段

时间:2020-09-27 23:33:48      阅读:82      评论:0      收藏:0      [点我收藏+]
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, ForeignKey, UniqueConstraint, Index
from sqlalchemy.orm import sessionmaker, relationship
from sqlalchemy import create_engine


Base = declarative_base()


# 创建单表
class Users(Base):
    __tablename__ = users
    id = Column(Integer, primary_key=True,autoincrement=True)
    name = Column(String(32))
    extra = Column(String(16))

    # __table_args__ = (
    #     UniqueConstraint(‘id‘, ‘name‘, name=‘uix_id_name‘),
    #     Index(‘ix_id_name‘, ‘name‘, ‘extra‘),
    # )


class Phone(Base):
    __tablename__ = Phone
    pid = Column(Integer, primary_key=True)
    name = Column(String(32))
    person = relationship(Person, backref=person)




def init_db():
    engine = create_engine("mysql+pymysql://root:qq995405033@127.0.0.1:3306/web", max_overflow=5)

    Base.metadata.create_all(engine)


def drop_db():
    engine = create_engine("mysql+pymysql://root:qq995405033@127.0.0.1:3306/web", max_overflow=5)

    Base.metadata.drop_all(engine)


if __name__ == __main__:
    drop_db()
    init_db()

 

【FLASK】使用ORM以及类似于Django更新新增字段

原文:https://www.cnblogs.com/wanghong1994/p/13742270.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!