SQLAlchemy

SQLAlchemy提供了一整套众所周知的企业级持久性模式,旨在实现高效,高性能的数据库访问,并采用简单的Pythonic域语言。(SQLAlchemy provides a full suite of well known enterprise-level persistence patterns, designed for efficient and high-performing database access, adapted into a simple and Pythonic domain language.)

Github stars Tracking Chart

SQLAlchemy是Python SQL工具包和对象关系映射器,它为应用程序开发人员提供了SQL的全部功能和灵活性。 SQLAlchemy提供了一整套众所周知的企业级持久性模式,旨在实现高效,高性能的数据库访问,并采用简单的Pythonic域语言。

主要的SQLAlchemy功能包括:

  • 一种工业强度ORM,由标识图上的核心、工作单元和数据映射器模式构建而成。这些模式允许使用声明性配置系统对对象进行透明持久化。可以自然地构造和操纵域模型,并且更改与当前事务自动同步。
  • 一个面向关系的查询系统,根据对象模型显式地公开SQL的所有功能,包括连接、子查询、关联和大多数其他功能。使用ORM编写查询使用的是编写SQL时使用的相关组合技术。虽然您可以随时使用文字SQL,但实际上从不需要它。
  • 一个全面而灵活的相关集合和对象的急切加载系统。集合在会话中缓存,可以在单个访问时加载,一次使用连接,或者在整个结果集中按集合查询。
  • 核心SQL构造系统和DBAPI交互层。 SQLAlchemy Core与ORM是独立的,它本身就是一个完整的数据库抽象层,包括一个可扩展的基于Python的SQL表达式语言、模式元数据、连接池、类型强制和自定义类型。
  • 假设所有主键和外键约束都是复合和自然的。代理整数主键当然仍然是常态,但SQLAlchemy从不假设或硬编码到此模型。
  • 数据库内省和生成。数据库模式可以一步“反映”到表示数据库元数据的Python结构中;然后,这些相同的结构可以立即生成CREATE语句 -- 所有这些都在Core内,独立于ORM。

SQLAlchemy的理念:

  • SQL数据库的行为越来越像对象集合,更大的尺寸和性能开始变得重要;对象集合的行为越来越少,就像表和行一样,抽象开始变得越来越重要。 SQLAlchemy旨在适应这两个原则。
  • ORM不需要隐藏“R”。关系数据库提供了应该完全公开的丰富的基于集合的功能。 SQLAlchemy的ORM提供了一组开放式模式,允许开发人员在域模型和关系模式之间构建自定义中介层,将所谓的“对象关系阻抗”问题转变为远程内存。
  • 在所有情况下,开发人员都会对对象模型以及关系模式的设计,结构和命名约定做出所有决策。 SQLAlchemy仅提供自动执行这些决策的方法。
  • 使用SQLAlchemy,没有“ORM生成错误查询”这一点 - 您可以完全控制查询结构,包括如何组织联接,如何使用子查询和关联,请求哪些列。 SQLAlchemy所做的一切最终都是由开发人员发起的决策的结果。
  • 如果问题不需要,请不要使用ORM。 SQLAlchemy由Core和单独的ORM组件组成。 Core提供了一个完整的SQL表达式语言,允许Pythonic构造SQL构造,直接呈现目标数据库的SQL字符串,返回本质上是增强型DBAPI游标的结果集。
  • 交易应该是常态。使用SQLAlchemy的ORM,在调用commit()之前,任何内容都不会永久存储。 SQLAlchemy鼓励应用程序创建一致的方法来描述一系列操作的开始和结束。
  • 永远不要在SQL语句中呈现文字值。最大程度地使用绑定参数,允许查询优化器有效地缓存查询计划并使SQL注入攻击成为非问题。

Overview

Name With Ownersqlalchemy/sqlalchemy
Primary LanguagePython
Program languagePython (Language Count: 1)
PlatformLinux, Mac, Windows
License:MIT License
Release Count308
Last Release Namerel_2_0_30 (Posted on )
First Release Namerel_0_1_0 (Posted on )
Created At2018-11-27 03:35:03
Pushed At2024-05-05 17:56:37
Last Commit At
Stargazers Count8.8k
Watchers Count97
Fork Count1.3k
Commits Count17k
Has Issues Enabled
Issues Count7749
Issue Open Count213
Pull Requests Count251
Pull Requests Open Count13
Pull Requests Close Count520
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

SQLAlchemy

The Python SQL Toolkit and Object Relational Mapper

Introduction

SQLAlchemy is the Python SQL toolkit and Object Relational Mapper
that gives application developers the full power and
flexibility of SQL. SQLAlchemy provides a full suite
of well known enterprise-level persistence patterns,
designed for efficient and high-performing database
access, adapted into a simple and Pythonic domain
language.

Major SQLAlchemy features include:

  • An industrial strength ORM, built
    from the core on the identity map, unit of work,
    and data mapper patterns. These patterns
    allow transparent persistence of objects
    using a declarative configuration system.
    Domain models
    can be constructed and manipulated naturally,
    and changes are synchronized with the
    current transaction automatically.
  • A relationally-oriented query system, exposing
    the full range of SQL's capabilities
    explicitly, including joins, subqueries,
    correlation, and most everything else,
    in terms of the object model.
    Writing queries with the ORM uses the same
    techniques of relational composition you use
    when writing SQL. While you can drop into
    literal SQL at any time, it's virtually never
    needed.
  • A comprehensive and flexible system
    of eager loading for related collections and objects.
    Collections are cached within a session,
    and can be loaded on individual access, all
    at once using joins, or by query per collection
    across the full result set.
  • A Core SQL construction system and DBAPI
    interaction layer. The SQLAlchemy Core is
    separate from the ORM and is a full database
    abstraction layer in its own right, and includes
    an extensible Python-based SQL expression
    language, schema metadata, connection pooling,
    type coercion, and custom types.
  • All primary and foreign key constraints are
    assumed to be composite and natural. Surrogate
    integer primary keys are of course still the
    norm, but SQLAlchemy never assumes or hardcodes
    to this model.
  • Database introspection and generation. Database
    schemas can be "reflected" in one step into
    Python structures representing database metadata;
    those same structures can then generate
    CREATE statements right back out - all within
    the Core, independent of the ORM.

SQLAlchemy's philosophy:

  • SQL databases behave less and less like object
    collections the more size and performance start to
    matter; object collections behave less and less like
    tables and rows the more abstraction starts to matter.
    SQLAlchemy aims to accommodate both of these
    principles.
  • An ORM doesn't need to hide the "R". A relational
    database provides rich, set-based functionality
    that should be fully exposed. SQLAlchemy's
    ORM provides an open-ended set of patterns
    that allow a developer to construct a custom
    mediation layer between a domain model and
    a relational schema, turning the so-called
    "object relational impedance" issue into
    a distant memory.
  • The developer, in all cases, makes all decisions
    regarding the design, structure, and naming conventions
    of both the object model as well as the relational
    schema. SQLAlchemy only provides the means
    to automate the execution of these decisions.
  • With SQLAlchemy, there's no such thing as
    "the ORM generated a bad query" - you
    retain full control over the structure of
    queries, including how joins are organized,
    how subqueries and correlation is used, what
    columns are requested. Everything SQLAlchemy
    does is ultimately the result of a developer-
    initiated decision.
  • Don't use an ORM if the problem doesn't need one.
    SQLAlchemy consists of a Core and separate ORM
    component. The Core offers a full SQL expression
    language that allows Pythonic construction
    of SQL constructs that render directly to SQL
    strings for a target database, returning
    result sets that are essentially enhanced DBAPI
    cursors.
  • Transactions should be the norm. With SQLAlchemy's
    ORM, nothing goes to permanent storage until
    commit() is called. SQLAlchemy encourages applications
    to create a consistent means of delineating
    the start and end of a series of operations.
  • Never render a literal value in a SQL statement.
    Bound parameters are used to the greatest degree
    possible, allowing query optimizers to cache
    query plans effectively and making SQL injection
    attacks a non-issue.

Documentation

Latest documentation is at:

http://www.sqlalchemy.org/docs/

Installation / Requirements

Full documentation for installation is at
Installation <http://www.sqlalchemy.org/docs/intro.html#installation>_.

Getting Help / Development / Bug reporting

Please refer to the SQLAlchemy Community Guide <http://www.sqlalchemy.org/support.html>_.

Code of Conduct

Above all, SQLAlchemy places great emphasis on polite, thoughtful, and
constructive communication between users and developers.
Please see our current Code of Conduct at
Code of Conduct <http://www.sqlalchemy.org/codeofconduct.html>_.

License

SQLAlchemy is distributed under the MIT license <http://www.opensource.org/licenses/mit-license.php>_.

To the top