logo资料库

Essential.SQLAlchemy.2nd.Edition.2015.11.pdf

第1页 / 共208页
第2页 / 共208页
第3页 / 共208页
第4页 / 共208页
第5页 / 共208页
第6页 / 共208页
第7页 / 共208页
第8页 / 共208页
资料共208页,剩余部分请下载后查看
Copyright
Table of Contents
Preface
Who This Book Is For
How to Use the Examples
Assumptions This Book Makes
Conventions Used in This Book
Using Code Examples
Safari® Books Online
How to Contact Us
Acknowledgments
Introduction to SQLAlchemy
Why Use SQLAlchemy?
SQLAlchemy Core and the SQL Expression Language
ORM
Choosing Between SQLAlchemy Core and ORM
Installing SQLAlchemy and Connecting to a Database
Installing Database Drivers
Connecting to a Database
Part I. SQLAlchemy Core
Chapter 1. Schema and Types
Types
Metadata
Tables
Columns
Keys and Constraints
Indexes
Relationships and ForeignKeyConstraints
Persisting the Tables
Chapter 2. Working with Data via SQLAlchemy Core
Inserting Data
Querying Data
ResultProxy
Controlling the Columns in the Query
Ordering
Limiting
Built-In SQL Functions and Labels
Filtering
ClauseElements
Operators
Boolean Operators
Conjunctions
Updating Data
Deleting Data
Joins
Aliases
Grouping
Chaining
Raw Queries
Chapter 3. Exceptions and Transactions
Exceptions
AttributeError
IntegrityError
Handling Errors
Transactions
Chapter 4. Testing
Testing with a Test Database
Using Mocks
Chapter 5. Reflection
Reflecting Individual Tables
Reflecting a Whole Database
Query Building with Reflected Objects
Part II. SQLAlchemy ORM
Chapter 6. Defining Schema with SQLAlchemy ORM
Defining Tables via ORM Classes
Keys, Constraints, and Indexes
Relationships
Persisting the Schema
Chapter 7. Working with Data via SQLAlchemy ORM
The Session
Inserting Data
Querying Data
Controlling the Columns in the Query
Ordering
Limiting
Built-In SQL Functions and Labels
Filtering
Operators
Boolean Operators
Conjunctions
Updating Data
Deleting Data
Joins
Grouping
Chaining
Raw Queries
Chapter 8. Understanding the Session and Exceptions
The SQLAlchemy Session
Session States
Exceptions
MultipleResultsFound Exception
DetachedInstanceError
Transactions
Chapter 9. Testing with SQLAlchemy ORM
Testing with a Test Database
Using Mocks
Chapter 10. Reflection with SQLAlchemy ORM and Automap
Reflecting a Database with Automap
Reflected Relationships
Part III. Alembic
Chapter 11. Getting Started with Alembic
Creating the Migration Environment
Configuring the Migration Environment
Chapter 12. Building Migrations
Generating a Base Empty Migration
Autogenerating a Migration
Building a Migration Manually
Chapter 13. Controlling Alembic
Determining a Database’s Migration Level
Downgrading Migrations
Marking the Database Migration Level
Generating SQL
Chapter 14. Cookbook
Hybrid Attributes
Association Proxy
Integrating SQLAlchemy with Flask
SQLAcodegen
Chapter 15. Where to Go from Here
Index
About the Authors
2 n d E ditio n Essential SQLAlchemy MAPPING PYTHON TO DATABASES Jason Myers & Rick Copeland
SECOND EDITION Essential SQLAlchemy Jason Myers and Rick Copeland Boston
Essential SQLAlchemy by Jason Myers and Rick Copeland Copyright © 2016 Jason Myers and Rick Copeland. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://safaribooksonline.com). For more information, contact our corporate/ institutional sales department: 800-998-9938 or corporate@oreilly.com. Editors: Dawn Schanafelt and Meghan Blanchette Production Editor: Shiny Kalapurakkel Copyeditor: Charles Roumeliotis Proofreader: Jasmine Kwityn Indexer: Angela Howard Interior Designer: David Futato Cover Designer: Karen Montgomery Illustrator: Rebecca Demarest June 2008: December 2015: First Edition Second Edition Revision History for the Second Edition 2015-11-20: First Release See http://oreilly.com/catalog/errata.csp?isbn=9781491916469 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Essential SQLAlchemy, the cover image, and related trade dress are trademarks of O’Reilly Media, Inc. While the publisher and the authors have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the authors disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights. 978-1-491-91646-9 [LSI]
Table of Contents Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vii Introduction to SQLAlchemy. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii Part I. SQLAlchemy Core 1. Schema and Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Types 1 Metadata 3 Tables 4 Columns 5 Keys and Constraints 6 Indexes 7 Relationships and ForeignKeyConstraints 7 Persisting the Tables 9 2. Working with Data via SQLAlchemy Core. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Inserting Data 13 Querying Data 17 ResultProxy 18 Controlling the Columns in the Query 20 Ordering 20 Limiting 21 Built-In SQL Functions and Labels 22 Filtering 24 ClauseElements 25 Operators 26 iii
Boolean Operators 27 Conjunctions 27 Updating Data 28 Deleting Data 29 Joins 31 Aliases 32 Grouping 33 Chaining 34 Raw Queries 35 3. Exceptions and Transactions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Exceptions 37 AttributeError 38 IntegrityError 40 Handling Errors 41 Transactions 43 4. Testing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Testing with a Test Database 51 Using Mocks 58 5. Reflection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Reflecting Individual Tables 63 Reflecting a Whole Database 66 Query Building with Reflected Objects 66 Part II. SQLAlchemy ORM 6. Defining Schema with SQLAlchemy ORM. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Defining Tables via ORM Classes 71 Keys, Constraints, and Indexes 73 Relationships 74 Persisting the Schema 75 7. Working with Data via SQLAlchemy ORM. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 The Session 77 Inserting Data 80 Querying Data 83 Controlling the Columns in the Query 86 Ordering 86 Limiting 87 iv | Table of Contents
Built-In SQL Functions and Labels 88 Filtering 89 Operators 90 Boolean Operators 92 Conjunctions 92 Updating Data 93 Deleting Data 94 Joins 97 Grouping 98 Chaining 99 Raw Queries 101 8. Understanding the Session and Exceptions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 The SQLAlchemy Session 105 Session States 105 Exceptions 108 MultipleResultsFound Exception 108 DetachedInstanceError 110 Transactions 112 9. Testing with SQLAlchemy ORM. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Testing with a Test Database 121 Using Mocks 130 10. Reflection with SQLAlchemy ORM and Automap. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Reflecting a Database with Automap 133 Reflected Relationships 135 Part III. Alembic 11. Getting Started with Alembic. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 Creating the Migration Environment 139 Configuring the Migration Environment 140 12. Building Migrations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 Generating a Base Empty Migration 143 Autogenerating a Migration 145 Building a Migration Manually 149 13. Controlling Alembic. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 Determining a Database’s Migration Level 151 Table of Contents | v
Downgrading Migrations 152 Marking the Database Migration Level 153 Generating SQL 154 14. Cookbook. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 Hybrid Attributes 157 Association Proxy 160 Integrating SQLAlchemy with Flask 166 SQLAcodegen 169 15. Where to Go from Here. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 vi | Table of Contents
分享到:
收藏