python之flash-SQLalchemy框架中,阅读源代码解决db.create_all的问题!

from flask import Flask
from kirin_app.kirin_web.book.view import blueprint_web
from kirin_app.models.database.db_book import db

app = Flask(__name__)

#配置文件必须放app创建完毕后的第一个位置,后续的挂在都是依赖配置文件来执行的!
# 把配置文件转换成一个dict字典对象
# 公开的信息的配置文件
app.config.from_object("kirin_app.config.settings")
#私有不能公开的信息的配置文件
app.config.from_object("kirin_app.config.secure")

#在app上注册一个blueprint_web蓝图
app.register_blueprint(blueprint_web)

#db数据库挂在到app对象上
db.init_app(app)
#创建所有的数据库表(推荐这种做法)
db.create_all(app=app)

/**
#另外一种create_all()不传值,但是让current_app有值的情况
with app.app_context():
    #手动推入APPcontext上下文,这样current_app就有值了!
    db.create_all()
**/

db.create_all方法要不要传app对象进入,我们查看官方源代码可以得知!

1、如果使用db.init_app(app)来挂载数据库,那么必须使用db.create_all(app=app),把app对象当做参数传入才能创建表成功!

原因是:init_app函数并没有把app对象保存在SQLAlchemy属性中,用完就不管了,可以查看源代码!(当然,特殊情况,在最下面一张图中,如果current_app存在的话,不传也可以,但是current_app必须手动推入上下文用with语句)

2、如果直接db.SQLAlchemy(app),相当于创建SQLAlchemy对象的时候已经传入了app,创建表的时候,可以省略!

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Product Description The demand for rich Internet applications (RIAs) such as complete storefronts and interactive surveys is skyrocketing, as is the pressure to create these dynamic apps overnight and at low cost. This in-depth Bible provides the step-by-step instructions you need to quickly create RIAs in Flash using cost-effective, open-source PHP programming tools. You’ll learn how PHP works, when you should use it in Flash, and above all, vital security techniques for keeping your interactive sites secure. From the Back Cover Quickly develop dynamic Flash applications with PHP The demand for rich Internet applications (RIAs) such as complete storefronts and interactive surveys is skyrocketing, as is the pressure to create these dynamic apps overnight and at low cost. This in-depth Bible provides the step-by-step instructions you need to quickly create RIAs in Flash using cost-effective, open-source PHP programming tools. You’ll learn how PHP works, when you should use it in Flash, and above all, vital security techniques for keeping your interactive sites secure. Install, configure, and explore the basics of PHP Connect Flash to PHP and PHP to MySQL Create contact forms and search capabilities Clean, store, and check your interactive data Apply advanced security techniques Develop an RSS reader or banner tracking with PHP Extend Flash and PHP with third-party libraries Learn best practices for backing up and updating your server Companion Web Site On the book’s Web site you can see real-world examples of Flash applications and how they were developed. The Web site also includes all the source files associated with the examples. Visit www.wiley.com/go/flashandphpbible.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值