sqlalchemy session

sessionmaker()

class sqlalchemy.orm.session.sessionmaker(bind=None, class_=<class 'sqlalchemy.orm.session.Session'>, autoflush=True, autocommit=False, expire_on_commit=True, info=None, **kw)
# global scope
Session = sessionmaker(autoflush=False)

# later, in a local scope, create and use a session:
sess = Session(bind=connection)

Session()

class sqlalchemy.orm.session.Session(bind=None, autoflush=True, expire_on_commit=True, _enable_transaction_accounting=True, autocommit=False, twophase=False, weak_identity_map=True, binds=None, extension=None, enable_baked_queries=True, info=None, query_cls=<class 'sqlalchemy.orm.query.Query'>)
方法/属性参数返回说明
add(instance, _warn=True)模型对象
add_all()添加数据
close()关闭回话
close_all()
commit()提交回话
dirty已经更新的对象
new新添加的对象
execute(clause, params=None, mapper=None, bind=None, **kw)orm,或者sql语句
query(*entities, **kwargs)查询对象
rollback()add之后回退
scalar(clause, params=None, mapper=None, bind=None, **kw)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1 Overview 3 1.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.2 Documentation Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.3 Code Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.4 Installation Guide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.4.1 Supported Platforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.4.2 Supported Installation Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.4.3 Install via easy_install or pip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.4.4 Installing using setup.py . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.4.5 Installing the C Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.4.6 Installing on Python 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.4.7 Installing a Database API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.4.8 Checking the Installed SQLAlchemy Version . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.5 0.6 to 0.7 Migration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 2 SQLAlchemy ORM 7 2.1 Object Relational Tutorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 2.1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 2.1.2 Version Check . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 2.1.3 Connecting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 2.1.4 Declare a Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 2.1.5 Create an Instance of the Mapped Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 2.1.6 Creating a Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 2.1.7 Adding New Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 2.1.8 Rolling Back . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 2.1.9 Querying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Common Filter Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Returning Lists and Scalars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Using Literal SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Counting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 2.1.10 Building a Relationship . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 2.1.11 Working with Related Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 2.1.12 Querying with Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Using Aliases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Using Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Selecting Entities from Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Using EXISTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Common Relationship Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 2.1.13 Eager Loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Subquery Load . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Joined Load . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Explicit Join + Eagerload . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 2.1.14 Deleting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Configuring delete/delete-orphan Cascade . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 2.1.15 Building a Many To Many Relationship . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 2.1.16 Further Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 2.2 Mapper Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 2.2.1 Classical Mappings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 2.2.2 Customizing Column Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Naming Columns Distinctly from Attribute Names . . . . . . . . . . . . . . . . . . . . . . . 40 Mapping Multiple Columns to a Single Attribute . . . . . . . . . . . . . . . . . . . . . . . . 41 Using column_property for column level options . . . . . . . . . . . . . . . . . . . . . . . . 42 Mapping a Subset of Table Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 2.2.3 Deferred Column Loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Column Deferral API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 2.2.4 SQL Expressions as Mapped Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Alternatives to column_property() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 2.2.5 Changing Attribute Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Simple Validators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Using Descriptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Synonyms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Custom Comparators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 2.2.6 Composite Column Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Tracking In-Place Mutations on Composites . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Redefining Comparison Operations for Composites . . . . . . . . . . . . . . . . . . . . . . . 56 2.2.7 Mapping a Class against Multiple Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 2.2.8 Mapping a Class against Arbitrary Selects . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 2.2.9 Multiple Mappers for One Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 2.2.10 Multiple “Persistence” Mappers for One Class . . . . . . . . . . . . . . . . . . . . . . . . . 58 2.2.11 Constructors and Object Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 2.2.12 Class Mapping API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 2.3 Relationship Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 2.3.1 Basic Relational Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 One To Many . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Many To One . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 One To One . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Many To Many . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Association Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 2.3.2 Adjacency List Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Self-Referential Query Strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Configuring Self-Referential Eager Loading . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 2.3.3 Linking Relationships with Backref . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Backref Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 One Way Backrefs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 2.3.4 Specifying Alternate Join Conditions to relationship() . . . . . . . . . . . . . . . . . . . . . 78 Self-Referential Many-to-Many Relationship . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Specifying Foreign Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Building Query-Enabled Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Multiple Relationships against the Same Parent/Child . . . . . . . . . . . . . . . . . . . . . . 81 2.3.5 Rows that point to themselves / Mutually Dependent Rows . . . . . . . . . . . . . . . . . . 82 2.3.6 Mutable Primary Keys / Update Cascades . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 2.3.7 Relationships API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 2.4 Collection Configuration and Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值