Release: 1.3.1 current release | Release Date: March 9, 2019

SQLAlchemy 1.3 Documentation

1.3 Changelog


no release date


  • [orm] [bug] Fixed bug where use of with_polymorphic() or other aliased construct would not properly adapt when the aliased target were used as the Select.correlate_except() target of a subquery used inside of a column_property(). This required a fix to the clause adaption mechanics to properly handle a selectable that shows up in the “correlate except” list, in a similar manner as which occurs for selectables that show up in the “correlate” list. This is ultimately a fairly fundamental bug that has lasted for a long time but it is hard to come across it.

    References: #4537


Released: March 9, 2019


  • [orm] [bug] [ext] Fixed regression where an association proxy linked to a synonym would no longer work, both at instance level and at class level.

    References: #4522


  • [mssql] [bug] A commit() is emitted after an isolation level change to SNAPSHOT, as both pyodbc and pymssql open an implicit transaction which blocks subsequent SQL from being emitted in the current transaction.

    This change is also backported to: 1.2.19

    References: #4536

  • [mssql] [bug] Fixed regression in SQL Server reflection due to #4393 where the removal of open-ended **kw from the Float datatype caused reflection of this type to fail due to a “scale” argument being passed.

    References: #4525


Released: March 4, 2019


  • [orm] [feature] The Query.get() method can now accept a dictionary of attribute keys and values as a means of indicating the primary key value to load; is particularly useful for composite primary keys. Pull request courtesy Sanjana S.

    References: #4316

  • [orm] [feature] A SQL expression can now be assigned to a primary key attribute for an ORM flush in the same manner as ordinary attributes as described in Embedding SQL Insert/Update Expressions into a Flush where the expression will be evaulated and then returned to the ORM using RETURNING, or in the case of pysqlite, works using the cursor.lastrowid attribute.Requires either a database that supports RETURNING (e.g. Postgresql, Oracle, SQL Server) or pysqlite.

    References: #3133


  • [engine] [feature] Revised the formatting for StatementError when stringified. Each error detail is broken up over multiple newlines instead of spaced out on a single line. Additionally, the SQL representation now stringifies the SQL statement rather than using repr(), so that newlines are rendered as is. Pull request courtesy Nate Clark.

    References: #4500


  • [sql] [bug] The Alias class and related subclasses CTE, Lateral and TableSample have been reworked so that it is not possible for a user to construct the objects directly. These constructs require that the standalone construction function or selectable-bound method be used to instantiate new objects.

    References: #4509


  • [schema] [feature] Added new parameters Table.resolve_fks and MetaData.reflect.resolve_fks which when set to False will disable the automatic reflection of related tables encountered in ForeignKey objects, which can both reduce SQL overhead for omitted tables as well as avoid tables that can’t be reflected for database-specific reasons. Two Table objects present in the same MetaData collection can still refer to each other even if the reflection of the two tables occurred separately.

    References: #4517


Released: February 8, 2019


  • [orm] [bug] Improved the behavior of orm.with_polymorphic() in conjunction with loader options, in particular wildcard operations as well as orm.load_only(). The polymorphic object will be more accurately targeted so that column-level options on the entity will correctly take effect.The issue is a continuation of the same kinds of things fixed in #4468.

    References: #4469

orm declarative

  • [bug] [declarative] [orm] Added some helper exceptions that invoke when a mapping based on AbstractConcreteBase, DeferredReflection, or AutoMap is used before the mapping is ready to be used, which contain descriptive information on the class, rather than falling through into other failure modes that are less informative.

    References: #4470


  • [sql] [bug] Fully removed the behavior of strings passed directly as components of a select() or Query object being coerced to text() constructs automatically; the warning that has been emitted is now an ArgumentError or in the case of order_by() / group_by() a CompileError. This has emitted a warning since version 1.0 however its presence continues to create concerns for the potential of mis-use of this behavior.

    Note that public CVEs have been posted for order_by() / group_by() which are resolved by this commit: CVE-2019-7164 CVE-2019-7548

    References: #4481

  • [sql] [bug] Quoting is applied to Function names, those which are usually but not necessarily generated from the sql.func construct, at compile time if they contain illegal characters, such as spaces or punctuation. The names are as before treated as case insensitive however, meaning if the names contain uppercase or mixed case characters, that alone does not trigger quoting. The case insensitivity is currently maintained for backwards compatibility.

    References: #4467

  • [sql] [bug] Added “SQL phrase validation” to key DDL phrases that are accepted as plain strings, including ForeignKeyConstraint.on_delete, ForeignKeyConstraint.on_update, ExcludeConstraint.using, ForeignKeyConstraint.initially, for areas where a series of SQL keywords only are expected.Any non-space characters that suggest the phrase would need to be quoted will raise a CompileError. This change is related to the series of changes committed as part of #4481.

    References: #4481


  • [postgresql] [bug] Fixed issue where using an uppercase name for an index type (e.g. GIST, BTREE, etc. ) or an EXCLUDE constraint would treat it as an identifier to be quoted, rather than rendering it as is. The new behavior converts these types to lowercase and ensures they contain only valid SQL characters.

    References: #4473


  • [bug] [ext] Implemented a more comprehensive assignment operation (e.g. “bulk replace”) when using association proxy with sets or dictionaries. Fixes the problem of redundant proxy objects being created to replace the old ones, which leads to excessive events and SQL and in the case of unique constraints will cause the flush to fail.

    References: #2642

  • [change] [tests] The test system has removed support for Nose, which is unmaintained for several years and is producing warnings under Python 3. The test suite is currently standardized on Pytest. Pull request courtesy Parth Shandilya.

    References: #4460


Released: January 25, 2019


  • [general] [change] A large change throughout the library has ensured that all objects, parameters, and behaviors which have been noted as deprecated or legacy now emit DeprecationWarning warnings when invoked.As the Python 3 interpreter now defaults to displaying deprecation warnings, as well as that modern test suites based on tools like tox and pytest tend to display deprecation warnings, this change should make it easier to note what API features are obsolete. A major rationale for this change is so that long- deprecated features that nonetheless still see continue to see real world use can finally be removed in the near future; the biggest example of this are the SessionExtension and MapperExtension classes as well as a handful of other pre-event extension hooks, which have been deprecated since version 0.7 but still remain in the library. Another is that several major longstanding behaviors are to be deprecated as well, including the threadlocal engine strategy, the convert_unicode flag, and non primary mappers.

    References: #4393


  • [orm] [feature] Implemented a new feature whereby the AliasedClass construct can now be used as the target of a relationship(). This allows the concept of “non primary mappers” to no longer be necessary, as the AliasedClass is much easier to configure and automatically inherits all the relationships of the mapped class, as well as preserves the ability for loader options to work normally.

    References: #4423

  • [orm] [feature] Added new MapperEvents.before_mapper_configured() event. This event complements the other “configure” stage mapper events with a per mapper event that receives each Mapper right before its configure step, and additionally may be used to prevent or delay the configuration of specific Mapper objects using a new return value orm.interfaces.EXT_SKIP. See the documentation link for an example.

    References: #4397

  • [orm] [bug] Fixed long-standing issue where duplicate collection members would cause a backref to delete the association between the member and its parent object when one of the duplicates were removed, as occurs as a side effect of swapping two objects in one statement.

    References: #1103

  • [orm] [bug] Extended the fix first made as part of #3287, where a loader option made against a subclass using a wildcard would extend itself to include application of the wildcard to attributes on the super classes as well, to a “bound” loader option as well, e.g. in an expression like Load(SomeSubClass).load_only('foo'). Columns that are part of the parent class of SomeSubClass will also be excluded in the same way as if the unbound option load_only('foo') were used.

    References: #4373

  • [orm] [bug] Improved error messages emitted by the ORM in the area of loader option traversal. This includes early detection of mis-matched loader strategies along with a clearer explanation why these strategies don’t match.

    References: #4433

  • [orm] [bug] The “remove” event for collections is now called before the item is removed in the case of the collection.remove() method, as is consistent with the behavior for most other forms of collection item removal (such as __delitem__, replacement under __setitem__). For pop() methods, the remove event still fires after the operation.

  • [orm] [bug] [engine] Added accessors for execution options to Core and ORM, via Query.get_execution_options(), Connection.get_execution_options(), Engine.get_execution_options(), and Executable.get_execution_options(). PR courtesy Daniel Lister.

    References: #4464

  • [orm] [bug] Fixed issue in association proxy due to #3423 which caused the use of custom PropComparator objects with hybrid attribites, such as the one demonstrated in the dictlike-polymorphic example to not function within an association proxy. The strictness that was added in #3423 has been relaxed, and additional logic to accomodate for an association proxy that links to a custom hybrid have been added.

    References: #4446

  • [orm] [bug] Implemented the .get_history() method, which also implies availability of AttributeState.history, for synonym() attributes. Previously, trying to access attribute history via a synonym would raise an AttributeError.

    References: #3777

  • [orm] [change] Added a new function close_all_sessions() which takes over the task of the Session.close_all() method, which is now deprecated as this is confusing as a classmethod. Pull request courtesy Augustin Trancart.

    References: #4412

orm declarative

  • [orm declarative] [bug] Added a __clause_element__() method to ColumnProperty which can allow the usage of a not-fully-declared column or deferred attribute in a declarative mapped class slightly more friendly when it’s used in a constraint or other column-oriented scenario within the class declaration, though this still can’t work in open-ended expressions; prefer to call the ColumnProperty.expression attribute if receiving TypeError.

    References: #4372


  • [engine] [feature] Added public accessor QueuePool.timeout() that returns the configured timeout for a QueuePool object. Pull request courtesy Irina Delamare.

    References: #3689

  • [engine] [change] The “threadlocal” engine strategy which has been a legacy feature of SQLAlchemy since around version 0.2 is now deprecated, along with the Pool.threadlocal parameter of Pool which has no effect in most modern use cases.

    References: #4393


  • [sql] [feature] Amended the AnsiFunction class, the base of common SQL functions like CURRENT_TIMESTAMP, to accept positional arguments like a regular ad-hoc function. This to suit the case that many of these functions on specific backends accept arguments such as “fractional seconds” precision and such. If the function is created with arguments, it renders the parenthesis and the arguments. If no arguments are present, the compiler generates the non-parenthesized form.

    References: #4386

  • [sql] [change] The create_engine.convert_unicode and String.convert_unicode parameters have been deprecated. These parameters were built back when most Python DBAPIs had little to no support for Python Unicode objects, and SQLAlchemy needed to take on the very complex task of marshalling data and SQL strings between Unicode and bytestrings throughout the system in a performant way. Thanks to Python 3, DBAPIs were compelled to adapt to Unicode-aware APIs and today all DBAPIs supported by SQLAlchemy support Unicode natively, including on Python 2, allowing this long-lived and very complicated feature to finally be (mostly) removed. There are still of course a few Python 2 edge cases where SQLAlchemy has to deal with Unicode however these are handled automatically; in modern use, there should be no need for end-user interaction with these flags.

    References: #4393


  • [mssql] [bug] The literal_processor for the Unicode and UnicodeText datatypes now render an N character in front of the literal string expression as required by SQL Server for Unicode string values rendered in SQL expressions.

    References: #4442


  • [bug] [ext] Fixed a regression in 1.3.0b1 caused by #3423 where association proxy objects that access an attribute that’s only present on a polymorphic subclass would raise an AttributeError even though the actual instance being accessed was an instance of that subclass.

    References: #4401


Released: November 16, 2018


  • [orm] [feature] Added new feature Query.only_return_tuples(). Causes the Query object to return keyed tuple objects unconditionally even if the query is against a single entity. Pull request courtesy Eric Atkin.

    This change is also backported to: 1.2.5

  • [orm] [feature] Added new flag Session.bulk_save_objects.preserve_order to the Session.bulk_save_objects() method, which defaults to True. When set to False, the given mappings will be grouped into inserts and updates per each object type, to allow for greater opportunities to batch common operations together. Pull request courtesy Alessandro Cucci.

  • [orm] [feature] The “selectin” loader strategy now omits the JOIN in the case of a simple one-to-many load, where it instead relies loads only from the related table, relying upon the foreign key columns of the related table in order to match up to primary keys in the parent table. This optimization can be disabled by setting the relationship.omit_join flag to False. Many thanks to Jayson Reis for the efforts on this.

    References: #4340

  • [orm] [feature] Added .info dictionary to the InstanceState class, the object that comes from calling inspect() on a mapped object.

    References: #4257

  • [orm] [bug] Fixed bug where use of Lateral construct in conjunction with Query.join() as well as Query.select_entity_from() would not apply clause adaption to the right side of the join. “lateral” introduces the use case of the right side of a join being correlatable. Previously, adaptation of this clause wasn’t considered. Note that in 1.2 only, a selectable introduced by Query.subquery() is still not adapted due to #4304; the selectable needs to be produced by the select() function to be the right side of the “lateral” join.

    This change is also backported to: 1.2.12

    References: #4334

  • [orm] [bug] Fixed issue regarding passive_deletes=”all”, where the foreign key attribute of an object is maintained with its value even after the object is removed from its parent collection. Previously, the unit of work would set this to NULL even though passive_deletes indicated it should not be modified.

    References: #3844

  • [orm] [bug] Improved the behavior of a relationship-bound many-to-one object expression such that the retrieval of column values on the related object are now resilient against the object being detached from its parent Session, even if the attribute has been expired. New features within the InstanceState are used to memoize the last known value of a particular column attribute before its expired, so that the expression can still evaluate when the object is detached and expired at the same time. Error conditions are also improved using modern attribute state features to produce more specific messages as needed.

    References: #4359

  • [orm] [bug] [mysql] [postgresql] The ORM now doubles the “FOR UPDATE” clause within the subquery that renders in conjunction with joined eager loading in some cases, as it has been observed that MySQL does not lock the rows from a subquery. This means the query renders with two FOR UPDATE clauses; note that on some backends such as Oracle, FOR UPDATE clauses on subqueries are silently ignored since they are unnecessary. Additionally, in the case of the “OF” clause used primarily with PostgreSQL, the FOR UPDATE is rendered only on the inner subquery when this is used so that the selectable can be targeted to the table within the SELECT statement.

    References: #4246

  • [orm] [bug] Refactored Query.join() to further clarify the individual components of structuring the join. This refactor adds the ability for Query.join() to determine the most appropriate “left” side of the join when there is more than one element in the FROM list or the query is against multiple entities. If more than one FROM/entity matches, an error is raised that asks for an ON clause to be specified to resolve the ambiguity. In particular this targets the regression we saw in #4363 but is also of general use. The codepaths within Query.join() are now easier to follow and the error cases are decided more specifically at an earlier point in the operation.

    References: #4365

  • [orm] [bug] Fixed long-standing issue in Query where a scalar subquery such as produced by Query.exists(), Query.as_scalar() and other derivations from Query.statement would not correctly be adapted when used in a new Query that required entity adaptation, such as when the query were turned into a union, or a from_self(), etc. The change removes the “no adaptation” annotation from the select() object produced by the Query.statement accessor.

    References: #4304

  • [orm] [bug] An informative exception is re-raised when a primary key value is not sortable in Python during an ORM flush under Python 3, such as an Enum that has no __lt__() method; normally Python 3 raises a TypeError in this case. The flush process sorts persistent objects by primary key in Python so the values must be sortable.

    References: #4232

  • [orm] [bug] Removed the collection converter used by the MappedCollection class. This converter was used only to assert that the incoming dictionary keys matched that of their corresponding objects, and only during a bulk set operation. The converter can interfere with a custom validator or AttributeEvents.bulk_replace() listener that wants to convert incoming values further. The TypeError which would be raised by this converter when an incoming key didn’t match the value is removed; incoming values during a bulk assignment will be keyed to their value-generated key, and not the key that’s explicitly present in the dictionary.

    Overall, @converter is superseded by the AttributeEvents.bulk_replace() event handler added as part of #3896.

    References: #3604

  • [orm] [bug] Added new behavior to the lazy load that takes place when the “old” value of a many-to-one is retrieved, such that exceptions which would be raised due to either lazy="raise" or a detached session error are skipped.

    References: #4353

  • [orm] [bug] A long-standing oversight in the ORM, the __delete__ method for a many- to-one relationship was non-functional, e.g. for an operation such as del a.b. This is now implemented and is equivalent to setting the attribute to None.

    References: #4354

orm declarative

  • [bug] [declarative] [orm] Fixed bug where declarative would not update the state of the Mapper as far as what attributes were present, when additional attributes were added or removed after the mapper attribute collections had already been called and memoized. Addtionally, a NotImplementedError is now raised if a fully mapped attribute (e.g. column, relationship, etc.) is deleted from a class that is currently mapped, since the mapper will not function correctly if the attribute has been removed.

    References: #4133


  • [engine] [feature] Added new “lifo” mode to QueuePool, typically enabled by setting the flag create_engine.pool_use_lifo to True. “lifo” mode means the same connection just checked in will be the first to be checked out again, allowing excess connections to be cleaned up from the server side during periods of the pool being only partially utilized. Pull request courtesy Taem Park.


  • [sql] [feature] Refactored SQLCompiler to expose a SQLCompiler.group_by_clause() method similar to the SQLCompiler.order_by_clause() and SQLCompiler.limit_clause() methods, which can be overridden by dialects to customize how GROUP BY renders. Pull request courtesy Samuel Chou.

    This change is also backported to: 1.2.13

  • [sql] [feature] Added Sequence to the “string SQL” system that will render a meaningful string expression ("<next sequence value: my_sequence>") when stringifying without a dialect a statement that includes a “sequence nextvalue” expression, rather than raising a compilation error.

    References: #4144

  • [sql] [feature] Added new naming convention tokens column_0N_name, column_0_N_name, etc., which will render the names / keys / labels for all columns referenced by a particular constraint in a sequence. In order to accommodate for the length of such a naming convention, the SQL compiler’s auto-truncation feature now applies itself to constraint names as well, which creates a shortened, deterministically generated name for the constraint that will apply to a target backend without going over the character limit of that backend.

    The change also repairs two other issues. One is that the column_0_key token wasn’t available even though this token was documented, the other was that the referred_column_0_name token would inadvertently render the .key and not the .name of the column if these two values were different.

    References: #3989

  • [sql] [feature] Added new logic to the “expanding IN” bound parameter feature whereby if the given list is empty, a special “empty set” expression that is specific to different backends is generated, thus allowing IN expressions to be fully dynamic including empty IN expressions.

    References: #4271

  • [sql] [feature] The Python builtin dir() is now supported for a SQLAlchemy “properties” object, such as that of a Core columns collection (e.g. .c), mapper.attrs, etc. Allows iPython autocompletion to work as well. Pull request courtesy Uwe Korn.

  • [sql] [feature] Added new feature FunctionElement.as_comparison() which allows a SQL function to act as a binary comparison operation that can work within the ORM.

    References: #3831

  • [sql] [bug] Added “like” based operators as “comparison” operators, including ColumnOperators.startswith() ColumnOperators.endswith() ColumnOperators.ilike() ColumnOperators.notilike() among many others, so that all of these operators can be the basis for an ORM “primaryjoin” condition.

    References: #4302

  • [sql] [bug] Fixed issue with TypeEngine.bind_expression() and TypeEngine.column_expression() methods where these methods would not work if the target type were part of a Variant, or other target type of a TypeDecorator. Additionally, the SQL compiler now calls upon the dialect-level implementation when it renders these methods so that dialects can now provide for SQL-level processing for built-in types.

    References: #3981


  • [postgresql] [feature] Added new PG type postgresql.REGCLASS which assists in casting table names to OID values. Pull request courtesy Sebastian Bank.

    This change is also backported to: 1.2.7

    References: #4160

  • [postgresql] [feature] Added rudimental support for reflection of PostgreSQL partitioned tables, e.g. that relkind=’p’ is added to reflection queries that return table information.

    References: #4237


  • [mysql] [feature] Support added for the “WITH PARSER” syntax of CREATE FULLTEXT INDEX in MySQL, using the mysql_with_parser keyword argument. Reflection is also supported, which accommodates MySQL’s special comment format for reporting on this option as well. Additionally, the “FULLTEXT” and “SPATIAL” index prefixes are now reflected back into the mysql_prefix index option.

    References: #4219

  • [mysql] [feature] Added support for the parameters in an ON DUPLICATE KEY UPDATE statement on MySQL to be ordered, since parameter order in a MySQL UPDATE clause is significant, in a similar manner as that described at Parameter-Ordered Updates. Pull request courtesy Maxim Bublis.

  • [mysql] [feature] The “pre-ping” feature of the connection pool now uses the ping() method of the DBAPI connection in the case of mysqlclient, PyMySQL and mysql-connector-python. Pull request courtesy Maxim Bublis.


  • [sqlite] [feature] Added support for SQLite’s json functionality via the new SQLite implementation for types.JSON, sqlite.JSON. The name used for the type is JSON, following an example found at SQLite’s own documentation. Pull request courtesy Ilja Everilä.

    References: #3850

  • [sqlite] [feature] Implemented the SQLite ON CONFLICT clause as understood at the DDL level, e.g. for primary key, unique, and CHECK constraints as well as specified on a Column to satisfy inline primary key and NOT NULL. Pull request courtesy Denis Kataev.

    References: #4360



  • [oracle] [feature] Added a new event currently used only by the cx_Oracle dialect, DialectEvents.setiputsizes(). The event passes a dictionary of BindParameter objects to DBAPI-specific type objects that will be passed, after conversion to parameter names, to the cx_Oracle cursor.setinputsizes() method. This allows both visibility into the setinputsizes process as well as the ability to alter the behavior of what datatypes are passed to this method.

    This change is also backported to: 1.2.9

    References: #4290

  • [oracle] [bug] Updated the parameters that can be sent to the cx_Oracle DBAPI to both allow for all current parameters as well as for future parameters not added yet. In addition, removed unused parameters that were deprecated in version 1.2, and additionally we are now defaulting “threaded” to False.

    References: #4369

  • [oracle] [bug] The Oracle dialect will no longer use the NCHAR/NCLOB datatypes represent generic unicode strings or clob fields in conjunction with Unicode and UnicodeText unless the flag use_nchar_for_unicode=True is passed to create_engine() - this includes CREATE TABLE behavior as well as setinputsizes() for bound parameters. On the read side, automatic Unicode conversion under Python 2 has been added to CHAR/VARCHAR/CLOB result rows, to match the behavior of cx_Oracle under Python 3. In order to mitigate the performance hit under Python 2, SQLAlchemy’s very performant (when C extensions are built) native Unicode handlers are used under Python 2.

    References: #4242


Previous: What’s New in SQLAlchemy 1.3? Next: 1.2 Changelog