在SQLAlchemy中,Session和db.session有什么区别? [英] What is the difference between Session and db.session in SQLAlchemy?

查看:87
本文介绍了在SQLAlchemy中,Session和db.session有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在事件映射器级别

In the event mapper level docs it says that Session.add() is not supported, but when I tried to do db.session.add(some_object) inside after_insert event it worked, example:

def after_insert_listener(mapper, connection, user):
    global_group = Group.query.filter_by(groupname='global').first()
    a = Association(user,global_group)
    db.session.add(a)

event.listen(User, 'after_insert', after_insert_listener)

基本上任何新用户都应该是global_group的一部分,因此我将其添加到了 after_insert 事件中.我尝试插入一个用户,然后将其检入数据库,并找到了用户记录和关联记录.

Basically any new user should be part of global_group, so I added it in the after_insert event. I tried to insert a user, and then checked into my database and I found the user record, and the association record.

推荐答案

让我们检查一下差异:

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://///Users/dedeco/Documents/tmp/testDb.db'
db = SQLAlchemy(app)

>>>type(db.session)
<class 'sqlalchemy.orm.scoping.scoped_session'>

from sqlalchemy import *
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base

some_engine = create_engine('sqlite://///Users/dedeco/Documents/tmp/testDb.db')
Session = sessionmaker(bind=some_engine)
session = Session()

Base = declarative_base()

>>> type(session)
<class 'sqlalchemy.orm.session.Session'>

基本上区别是:

  • 使用第一种方法,是使用为Flask框架开发的API,称为Flask-SQLAlchemy.如果要创建Flask应用程序,则可以选择此选项,因为Session的范围可以由您的应用程序自动管理.您有很多好处,例如用于建立与请求相关联的单个会话的基础结构,该基础结构被正确构造并在请求结束时被拆除并被拆除.

  • In the first way you are using a API developed for the Flask framework, called Flask-SQLAlchemy. It's the option if you are creating a Flask application, because the scope of the Session can be managed automatically by your application. You have many benefits like a infrastructure to establish a single Session, associated with the request, which is correctly constructed and torn down corresponding torn down at the end of a request.

第二种方式是纯SQLAlchemy应用程序,因此,如果您使用库来连接特定的数据库,则只能使用SQLAlchemy API,例如,用于命令行脚本,后台守护程序,GUI界面驱动的应用程序等.

In the second way is a pure SQLAlchemy app, so if you are using a library to connect a particular database, you can use just a SQLAlchemy API, for example, for a command-line script, background daemon, GUI interface-driven application, etc.

因此,两种方式都可以添加,例如:

So, in a both way you can add, like:

使用 Flask-SQLAlchemy :

class User(db.Model):
    __tablename__ = 'users'
    user_id = db.Column(db.Integer(), primary_key = True)
    user_name = db.Column(db.String(80), unique=True)
    def __init__(self, user_name):
        self.user_name = user_name

>>> db.create_all()
>>> u = User('user1')
>>> db.session.add(u)
>>> db.session.commit()
>>> users = db.session.query(User).all()
>>> for u in users:
...     print u.user_name
... 
user1

仅使用 SQLAlchemy :

class User(Base):
    __tablename__ = 'users'
    user_id = Column(Integer(), primary_key = True)
    user_name = Column(String(80), unique=True)

>>> u = User()
>>> u.user_name = 'user2'
>>> session.add(u)
>>> session.commit()
>>> users = session.query(User).all()
>>> for u in users:
...     print u.user_name
... 
user1
user2

意识到我正在连接同一数据库,只是为了表明您可以使用多种方式添加.

Realize that I am connecting in the same database just for show that you can add using many ways.

这篇关于在SQLAlchemy中,Session和db.session有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆