http://docs.sqlalchemy.org/en/rel_0_7/index.html
http://docs.sqlalchemy.org/en/rel_0_7/orm/tutorial.html
- The SQL Expression Language represents the relational database directly without opinion; the ORM builds upon it a high level and abstracted pattern of usage.
- The ORM approaches structure and content of data from a user-defined domain model, transparently persisted and refreshed from its underlying storage model.
- An
Engineinstance is a core interface to the database, adapted to the DBAPI in use. - The declarative base class maintains a catalog of classes and tables relative to that base.
- SQLAlchemy never makes assumptions about names or characteristics of data; the developer must be explicit about specific conventions in use.
- The declarative base class has a
metadataattribute that contains a catalog of all theTableobjects that have been defined. - The declarative system supplies a default constructor which accepts keyword arguments of the same name as that of the mapped attributes.
- The
Sessionobject is just a workspace for your objects, local to a particular database connection. - Adding an entity to a
Sessiondoes not persist it to the database; this is only done when the session is flushed. - Using the identity map, once an object is in a
Session, all queries with its particular primary key return that same object. - By default, data from a previous transaction is refreshed the first time it’s accessed within a new transaction, so that it is not stale.
- The tuples returned by a
Queryobject are named tuples, and can be treated much like an ordinary Python object. - A
Queryobject is fully generative, so most method calls return a newQueryobject upon which further criteria may be added. - The
one()method ofQueryfully fetches all rows, and if not exactly one object identity or composite row is returned, raises an error. - The
relationship()method uses the foreign key relationships between the two tables to determine the nature of this linkage. - A foreign key constraint in most (though not all) relational databases can only link to a primary key column, or a column with a
UNIQUEconstraint. - A foreign key constraint that refers to a multiple-column primary key, or a subset of one, and itself has multiple columns, is called a composite foreign key.
- Objects defined in a relationship use lazy loading by default.
- The
aliasedconstruct supports aliasing a table with another name so that it can be distinguished against other occurences of that table in a query. - The
subquerymethod returns a SQL expression construct representing aSELECTstatement embedded in an alias, and behaves like aTableconstruct. - The
orm.subqueryload()option is called so because theSELECTstatement constructed is embedded as a subquery into aSELECTagainst the related table. - The
orm.joinedload()option emits a left outer join so that the lead object and the related object or collection is loaded in one step. subqueryloadis appropriate for related collections, whilejoinedloadis better suited for many-to-one relationships.- The
contains_eager()function is typically most useful for pre-loading the many-to-one object on a query that needs to filter on that same object. - By default deletes don't cascade; instead you must explicitly specify this when establishing a relationship.
- If a many-to-many relationship has any columns other than foreign keys, you must use the "association object" pattern.
http://docs.sqlalchemy.org/en/rel_0_7/core/tutorial.html
- The expression language allows writing backend-neutral SQL expressions, but does not attempt to enforce that expressions are backend-neutral.
- Table reflection allows "importing" whole sets of
Tableobjects automatically from an existing database. - An
Insertobject does not render bound data in its string form; to see this, callcompile()and access itsparamsattribute. - SQLAlchemy always returns any newly generated primary key value in a returned
ResultProxy, regardless of the SQL dialect. - The
Insertstatement is compiled against the first dictionary in a list of values to insert; subsequent dictionaries must have the same keys. - Call
closeon aResultProxyobject when done to release cursor and connection resources. - The
Columntype determines how an operator is translated to SQL; for example, aStringinterprets+as string concatenation. - The
labelmethod produces labels using theASkeyword, and allows selecting from expressions that otherwise don't have a name. - Any
Table,selectconstructor, or selectable can be turned into an alias using itsaliasmethod. - The
ONcondition in the SQL produced from a call tojoin()is automatically generated from theForeignKeycolumn of either table. - The
outerjoin()method createsLEFT OUTER JOINconstructs in SQL. - Use
bindparam()to create bind parameters in a statement; their values are only provided when executing, allowing you to reuse the statement. - ANSI functions are part of the functions known by SQLAlchemy, and are special in that they don't have parentheses added after them.
- Label a function to allow extracting its result from a returned row; assign it a type to enable result-set processing, such as Unicode or date conversions.
- To embed a
SELECTin a column expression, useas_scalar()or apply alabel()to it. - SQLAlchemy automatically correlates embedded
FROMobjects to that of an enclosing query; to disable or specify explicitFROMclauses, usecorrelate(). - A correlated update lets you update a table using a selection from another table, or the same table.
- The Postgresql, Microsoft SQL Server, and MySQL backends support
UPDATEstatements that refer to multiple tables.
http://docs.sqlalchemy.org/en/rel_0_7/orm/session.html
- A
Sessionrequests a connection from theEngine, which represents an ongoing transaction until it commits or rolls back its pending state. - Objects associated with a
Sessionare proxy objects, and there exist events that cause these objects to re-access the database and keep synchronized. - You get persistent object instances either by flushing pending instances so they become persistent, or querying for existing instances.
- A session can span multiple transactions, which must proceed serially; this is called transaction scope and session scope.
- In a web application, typically the scope of a
Sessionis tied to the lifetime of the request, maybe using event hooks in the web framework. - A
Sessionis similar to a cache in that it implements the identity map, but only uses the map as a cache if you query by a primary key. - By default a
Sessionexpires all instances along transaction boundaries, so no instance should have stale data. - When merging, if a mapped attribute is missing on the source instance, then it is expired on the target instance, discarding its existing value.
- The
loadflag when merging isTrueby default, which loads the target's unloaded collections to reconcile the incoming state against the database. - Most
merge()issues can be fixed by asserting that the object is not in the session, or removing unwanted state from its__dict__. - To delete items in a collection, use cascade behavior to automatically invoke the deletion as a result of removing objects from the parent collection.
- A
flush()occurs before any issuedQueryifautoflushis changed toTrue, as well as within thecommit()call for the transaction. - If
flush()fails, then the database transaction has been rolled back, but to reset the state of theSessionyou must still callrollback(). - Methods
refresh()andexpire()are usually only necessary after issuing anUPDATEorDELETEusingSession.execute(). - Typically cascade is left as its default of
save-update, merge, or set asall, delete-orphanto treat child objects as "owned" by the parent. - Without the
deletecascade option, SQLAlchemy will set a foreign key on a one-to-many relationship toNULLwhen the parent object is deleted. - Autocommit mode should not be considered for general use, but frameworks may use it to control when a transaction begins.
- Two-phase commits, supported by MySQL and PostgreSQL, ensure that a transaction is either committed or rolled back in all databases.
- You can assign a SQL expression instead of a literal value to an object's attribute, useful for atomic updates or calling stored procedures.
- Joining a
Sessionwith an externalConnectionandTransactionallows a test'stearDownmethod to rollback subtransactions by its methods. - A
ScopedSessionreturns the sameSessionobject until it is disposed of; methods called onScopedSessionalso proxy to thatSessionobject. - Overriding
Session.get_bind()allows custom vertical partitioning, such as directing write operations to a master and read operations to slaves.
- Constructor options:
autocommit: Defaults toFalse; whenTrue, no persistent transaction is used, and connections are released right after use.autoflush: Defaults toTrue, where allquery()operations callflush()before proceeding; typically used whenautocommitisFalse.
close(): Closes the session, clears all items and ends the transaction; ifautocommitisTrue, a new transaction is immediately started.execute(): Executes within the transaction, but does not invokeautoflush.expunge(): Removes an instance from the session, freeing all internal resources to it.flush(): Writes all pendingINSERT,UPDATE, andDELETEoperations to the transaction buffer; if an error occurs, it is rolled back.is_active:Falseifflushencountered an error, and theSessionawaits the user to callrollbackto close out the transaction stack.merge(): If the objct to reconcile is not in the session, it attempts to retrieve it, or else creates it.no_autoflush(): Returns a context manager that hasautoflushset toFalse.refresh(): This method only needs to be called if a non-ORM SQL statement were emitted in the transaction, orautocommitisTrue.
http://docs.sqlalchemy.org/en/rel_0_7/orm/relationships.html
- An association table used with the
secondaryargument torelationship()is automatically subject toINSERTandDELETEstatements. - The association pattern requires that child objects are associated with an associated instance before being appended to the parent.
- The association proxy allows accessing an attribute of an associated object in one "hop" instead of two.
- An adjacency list pattern is where a table has a foreign key reference to itself, and is used to represent hierarchical data in flat tables.
Query.join()has analiasedparameter that shortens the verbosity of self-referential joins, at the expense of query flexibility.- The
join_depthparameter specifies how many levels deep to join or query when using eager loading with a self-referential relationship. - Keyword
backrefadds a secondrelationshipon the other side, and adds event listeners to monitor attribute operations in both directions. - For a collection that contains a filtering
primaryjoincondition, a one-way backref ensures that all collection items pass the filter. - If there are multiple ways to join two tables, each
relationship()must specify aprimaryjointo resolve the ambiguity. - Objects in a colleciton that fail some
primaryjoincriteria will remain until the attribute is expired and reloaded from the database. - If
backrefcreates a bidirectional, self-referential many-to-many relationship, it reverses theprimaryjoinandsecondaryjoinarguemnts. - The
post_updateoption ofrelationship()relates two rows using anUPDATEstatement on their foreign keys after each is inserted. - Setting
passive_updatestoFalseissues the necessaryUPDATEstatements if primary key changes are not propagated to foreign keys.
back_populates: Likebackrefexcept the complementing property must be configured explicitly and also includeback_populates.- Cascade
delete-orphanwon't prevent persisting a child without a parent; set the child's foreign key asNOT NULLto ensure this. cascade_backrefs: Defaults toTrue; ifFalse, assigning a value in the session to the attribute of a transient won't make it pending.innerjoin: Defaults toFalse; ifTrue, eager loads use inner and not outer joins, which generally perform better.lazy: Defaults toselectwhich lazily loads;joinedandsubqueryeagerly load by joins;dynamicreturns aQueryobject.order_by: Specifies the ordering to apply when the items are loaded.passive_deletes: Defaults toFalse; ifTrue, implies that anON DELETErule is in place to update or delete child rows.post_update: If aflush()operation returns a "cyclical dependency" error, you may usepost_updateto break the cycle.secondaryjoin: Expression used as the join of an association table to the child object, whileprimaryjoinuses the parent object.single_parent: Installs a validator to ensure only one parent; its usage is required ifdelete-orphancascade is set.viewonly: Defaults toFalse; ifTrue, the relationship is only for loading, and has no effect on the unit-of-work flush process.
http://docs.sqlalchemy.org/en/rel_0_7/core/connections.html
- An
Engineinstance manages many DBAPI connections and is intended to be called upon in a concurrent fashion. - A
ResultProxythat returns no rows, such as that of anUPDATEstatement, relases cursor resources immediately upon construction. - Closing a
ResultProxyalso closes theConnection, returning the DBAPI connection to the pool and releasing transactional resources. - Nesting transaction blocks creates a new transaction if one is not available, or participates in an enclosing transaction if one exists.
- PEP-0249 says that a transaction is always in progress, so
autocommit=Trueissues aCOMMITautomatically if one was not created. - Implicit execution is discouraged and is not appropriate for use with the ORM, as it bypasses the transactional context of
Session.
http://docs.sqlalchemy.org/en/rel_0_7/orm/collections.html
- By default, when a parent is deleted, the
Sessionloads all its children, to either delete them or set their foreign key toNULL. - A dynamic form of
relationshipreturns aQueryobject in place of a collection when accessed, and works with collections only. - Using
passive_deleteswon't load children when a parent is marked for deletion;ON DELETE CASCADEcan then delegate integrity to the database. - Sets, mutable sequences, or almost any other container-like class can be used in place of the default list for a
relationship(). - When using a dictionary collection, it's easiest to use
attribute_mapped_collectionwith a@property. - Dictionary mappings are often combined with the association proxy extension to produce streamlined dictionary views.
- Lists, sets, and dictionaries and their subclasses are automatically instrumented; add the
__emulates__class attribute to all others. - The
appender,remover, anditeratordecorators adapt any container class with different method names than its emulated class.
http://docs.sqlalchemy.org/en/rel_0_7/orm/loading.html
- The loader strategy specified by the
lazykeyword for arelationship()can be overridden on a per-query basis. - Dot-separated names apply only to the acutal attribute named; to also apply to ancestors, use
joinedload_all()orsubqueryload_all(). - Passing
*to a loader option sets a default strategy, but it does not supercede other loader options stated in the query. joinedload()creates an anonymous alias of the joins it adds, so they can't be referenced by other parts of the query.- Queries with modifiers affecting the rows returned are run as subqueries, to which joins for eager loading are then applied to.
- In a one-to-many relationship:
- A
LEFT OUTER JOINreturns rows for empty collections and repeats parent columns for each child, so use it when collections are small. - A subquery load uses an
INNER JOINand a minimum of parent columns are returned, so use it for large collections.
- A
- In a many-to-one relationship:
- If a small set of target objects is referenced, then lazy-loading can exploit the caching property of the identity map.
- If you know the foreign key reference is not
NULL, you can perform a joined load withinnerjoin=Truewhich is very efficient.
- For a query with the necessary joins, option
contains_eager()populates references and collections without a superfluousjoinedload().
contains_eager(): Indicates that an attribute should be eagerly loaded from columns created by an explicit join in the query.joinedload(): Its joins are anonymously aliased, affecting how related objects or collections are loaded without impacting the results.