Sophie

Sophie

distrib > Mageia > 7 > armv7hl > by-pkgid > b0b6ffab06cbeede296e36ce94734bf8 > files > 865

python3-sqlalchemy-1.2.19-1.mga7.armv7hl.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        
        <title>
            
    
    Session API
 &mdash;
    SQLAlchemy 1.2 Documentation

        </title>

        
            <!-- begin iterate through site-imported + sphinx environment css_files -->
                <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
                <link rel="stylesheet" href="../_static/docs.css" type="text/css" />
                <link rel="stylesheet" href="../_static/changelog.css" type="text/css" />
                <link rel="stylesheet" href="../_static/sphinx_paramlinks.css" type="text/css" />
            <!-- end iterate through site-imported + sphinx environment css_files -->
        

        

    

    <!-- begin layout.mako headers -->

    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
        <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="SQLAlchemy 1.2 Documentation" href="../index.html" />
        <link rel="up" title="Using the Session" href="session.html" />
        <link rel="next" title="Events and Internals" href="extending.html" />
        <link rel="prev" title="Tracking Object and Session Changes with Events" href="session_events.html" />
    <!-- end layout.mako headers -->


    </head>
    <body>
        
















<div id="docs-container">





<div id="docs-top-navigation-container" class="body-background">
<div id="docs-header">
    <div id="docs-version-header">
        Release: <span class="version-num">1.2.19</span>


        | Release Date: April 15, 2019

    </div>

    <h1>SQLAlchemy 1.2 Documentation</h1>

</div>
</div>

<div id="docs-body-container">

    <div id="fixed-sidebar" class="withsidebar">


        <div id="docs-sidebar-popout">
            <h3><a href="../index.html">SQLAlchemy 1.2 Documentation</a></h3>
            <p id="sidebar-topnav">
                <a href="../contents.html">Contents</a> |
                <a href="../genindex.html">Index</a>
            </p>

            <div id="sidebar-search">
                <form class="search" action="../search.html" method="get">
                  <label>
                  Search terms:
                  <input type="text" placeholder="search..." name="q" size="12" />
                  </label>
                  <input type="hidden" name="check_keywords" value="yes" />
                  <input type="hidden" name="area" value="default" />
                </form>
            </div>

        </div>

        <div id="docs-sidebar">

        <div id="sidebar-banner">
            
        </div>

        <div id="docs-sidebar-inner">

        
        <h3>
            <a href="index.html" title="SQLAlchemy ORM">SQLAlchemy ORM</a>
        </h3>

        <ul>
<li><span class="link-container"><a class="reference external" href="tutorial.html">Object Relational Tutorial</a></span></li>
<li><span class="link-container"><a class="reference external" href="mapper_config.html">Mapper Configuration</a></span></li>
<li><span class="link-container"><a class="reference external" href="relationships.html">Relationship Configuration</a></span></li>
<li><span class="link-container"><a class="reference external" href="loading_objects.html">Loading Objects</a></span></li>
<li><span class="link-container"><a class="reference external" href="session.html">Using the Session</a></span><ul>
<li><span class="link-container"><a class="reference external" href="session_basics.html">Session Basics</a></span></li>
<li><span class="link-container"><a class="reference external" href="session_state_management.html">State Management</a></span></li>
<li><span class="link-container"><a class="reference external" href="cascades.html">Cascades</a></span></li>
<li><span class="link-container"><a class="reference external" href="session_transaction.html">Transactions and Connection Management</a></span></li>
<li><span class="link-container"><a class="reference external" href="persistence_techniques.html">Additional Persistence Techniques</a></span></li>
<li><span class="link-container"><a class="reference external" href="contextual.html">Contextual/Thread-local Sessions</a></span></li>
<li><span class="link-container"><a class="reference external" href="session_events.html">Tracking Object and Session Changes with Events</a></span></li>
<li class="selected"><span class="link-container"><strong>Session API</strong><a class="paramlink headerlink reference internal" href="#">¶</a></span><ul>
<li><span class="link-container"><a class="reference external" href="#session-and-sessionmaker">Session and sessionmaker()</a></span></li>
<li><span class="link-container"><a class="reference external" href="#session-utilities">Session Utilities</a></span></li>
<li><span class="link-container"><a class="reference external" href="#attribute-and-state-management-utilities">Attribute and State Management Utilities</a></span></li>
</ul>
</li>
</ul>
</li>
<li><span class="link-container"><a class="reference external" href="extending.html">Events and Internals</a></span></li>
<li><span class="link-container"><a class="reference external" href="extensions/index.html">ORM Extensions</a></span></li>
<li><span class="link-container"><a class="reference external" href="examples.html">ORM Examples</a></span></li>
</ul>



        </div>

        </div>

    </div>

    

    <div id="docs-body" class="withsidebar" >
        
<span class="target" id="module-sqlalchemy.orm.session"></span><div class="section" id="session-api">
<h1>Session API<a class="headerlink" href="#session-api" title="Permalink to this headline">¶</a></h1>
<div class="section" id="session-and-sessionmaker">
<h2>Session and sessionmaker()<a class="headerlink" href="#session-and-sessionmaker" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="sqlalchemy.orm.session.sessionmaker">
<em class="property">class </em><code class="descclassname">sqlalchemy.orm.session.</code><code class="descname">sessionmaker</code><span class="sig-paren">(</span><em>bind=None</em>, <em>class_=&lt;class 'sqlalchemy.orm.session.Session'&gt;</em>, <em>autoflush=True</em>, <em>autocommit=False</em>, <em>expire_on_commit=True</em>, <em>info=None</em>, <em>**kw</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.sessionmaker" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <code class="xref py py-class docutils literal notranslate"><span class="pre">sqlalchemy.orm.session._SessionClassMethods</span></code></p>
<p>A configurable <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> factory.</p>
<p>The <a class="reference internal" href="#sqlalchemy.orm.session.sessionmaker" title="sqlalchemy.orm.session.sessionmaker"><code class="xref py py-class docutils literal notranslate"><span class="pre">sessionmaker</span></code></a> factory generates new
<a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> objects when called, creating them given
the configurational arguments established here.</p>
<p>e.g.:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># global scope</span>
<span class="n">Session</span> <span class="o">=</span> <span class="n">sessionmaker</span><span class="p">(</span><span class="n">autoflush</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>

<span class="c1"># later, in a local scope, create and use a session:</span>
<span class="n">sess</span> <span class="o">=</span> <span class="n">Session</span><span class="p">()</span></pre></div>
</div>
<p>Any keyword arguments sent to the constructor itself will override the
“configured” keywords:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Session</span> <span class="o">=</span> <span class="n">sessionmaker</span><span class="p">()</span>

<span class="c1"># bind an individual session to a connection</span>
<span class="n">sess</span> <span class="o">=</span> <span class="n">Session</span><span class="p">(</span><span class="n">bind</span><span class="o">=</span><span class="n">connection</span><span class="p">)</span></pre></div>
</div>
<p>The class also includes a method <a class="reference internal" href="#sqlalchemy.orm.session.sessionmaker.configure" title="sqlalchemy.orm.session.sessionmaker.configure"><code class="xref py py-meth docutils literal notranslate"><span class="pre">configure()</span></code></a>, which can
be used to specify additional keyword arguments to the factory, which
will take effect for subsequent <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> objects generated.
This is usually used to associate one or more <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Engine</span></code></a> objects
with an existing <a class="reference internal" href="#sqlalchemy.orm.session.sessionmaker" title="sqlalchemy.orm.session.sessionmaker"><code class="xref py py-class docutils literal notranslate"><span class="pre">sessionmaker</span></code></a> factory before it is first
used:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># application starts</span>
<span class="n">Session</span> <span class="o">=</span> <span class="n">sessionmaker</span><span class="p">()</span>

<span class="c1"># ... later</span>
<span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s1">&#39;sqlite:///foo.db&#39;</span><span class="p">)</span>
<span class="n">Session</span><span class="o">.</span><span class="n">configure</span><span class="p">(</span><span class="n">bind</span><span class="o">=</span><span class="n">engine</span><span class="p">)</span>

<span class="n">sess</span> <span class="o">=</span> <span class="n">Session</span><span class="p">()</span></pre></div>
</div>
<dl class="method">
<dt id="sqlalchemy.orm.session.sessionmaker.__call__">
<code class="descname">__call__</code><span class="sig-paren">(</span><em>**local_kw</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.sessionmaker.__call__" title="Permalink to this definition">¶</a></dt>
<dd><p>Produce a new <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> object using the configuration
established in this <a class="reference internal" href="#sqlalchemy.orm.session.sessionmaker" title="sqlalchemy.orm.session.sessionmaker"><code class="xref py py-class docutils literal notranslate"><span class="pre">sessionmaker</span></code></a>.</p>
<p>In Python, the <code class="docutils literal notranslate"><span class="pre">__call__</span></code> method is invoked on an object when
it is “called” in the same way as a function:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Session</span> <span class="o">=</span> <span class="n">sessionmaker</span><span class="p">()</span>
<span class="n">session</span> <span class="o">=</span> <span class="n">Session</span><span class="p">()</span>  <span class="c1"># invokes sessionmaker.__call__()</span></pre></div>
</div>
</dd></dl>

<dl class="attribute">
<dt id="sqlalchemy.orm.session.sessionmaker.__eq__">
<code class="descname">__eq__</code><a class="headerlink" href="#sqlalchemy.orm.session.sessionmaker.__eq__" title="Permalink to this definition">¶</a></dt>
<dd><div class="inherited-member docutils container">
<p><em>inherited from the</em> <code class="xref py py-attr docutils literal notranslate"><span class="pre">__eq__</span></code> <em>attribute of</em> <code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></p>
</div>
<p>Return self==value.</p>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.orm.session.sessionmaker.__init__">
<code class="descname">__init__</code><span class="sig-paren">(</span><em>bind=None</em>, <em>class_=&lt;class 'sqlalchemy.orm.session.Session'&gt;</em>, <em>autoflush=True</em>, <em>autocommit=False</em>, <em>expire_on_commit=True</em>, <em>info=None</em>, <em>**kw</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.sessionmaker.__init__" title="Permalink to this definition">¶</a></dt>
<dd><p>Construct a new <a class="reference internal" href="#sqlalchemy.orm.session.sessionmaker" title="sqlalchemy.orm.session.sessionmaker"><code class="xref py py-class docutils literal notranslate"><span class="pre">sessionmaker</span></code></a>.</p>
<p>All arguments here except for <code class="docutils literal notranslate"><span class="pre">class_</span></code> correspond to arguments
accepted by <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> directly.  See the
<a class="reference internal" href="#sqlalchemy.orm.session.Session.__init__" title="sqlalchemy.orm.session.Session.__init__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.__init__()</span></code></a> docstring for more details on parameters.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><span class="target" id="sqlalchemy.orm.session.sessionmaker.params.bind"></span><strong>bind</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.sessionmaker.params.bind">¶</a> – a <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Engine</span></code></a> or other <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Connectable" title="sqlalchemy.engine.Connectable"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connectable</span></code></a> with
which newly created <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> objects will be associated.</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.sessionmaker.params.class_"></span><strong>class_</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.sessionmaker.params.class_">¶</a> – class to use in order to create new <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a>
objects.  Defaults to <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a>.</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.sessionmaker.params.autoflush"></span><strong>autoflush</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.sessionmaker.params.autoflush">¶</a> – The autoflush setting to use with newly created
<a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> objects.</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.sessionmaker.params.autocommit"></span><strong>autocommit</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.sessionmaker.params.autocommit">¶</a> – The autocommit setting to use with newly created
<a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> objects.</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.sessionmaker.params.expire_on_commit"></span><strong>expire_on_commit=True</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.sessionmaker.params.expire_on_commit">¶</a> – the expire_on_commit setting to use
with newly created <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> objects.</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.sessionmaker.params.info"></span><strong>info</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.sessionmaker.params.info">¶</a> – <p>optional dictionary of information that will be available
via <a class="reference internal" href="#sqlalchemy.orm.session.Session.info" title="sqlalchemy.orm.session.Session.info"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Session.info</span></code></a>.  Note this dictionary is <em>updated</em>, not
replaced, when the <code class="docutils literal notranslate"><span class="pre">info</span></code> parameter is specified to the specific
<a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> construction operation.</p>
<div class="versionadded">
<p><span class="versionmodified added">New in version 0.9.0.</span></p>
</div>
</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.sessionmaker.params.**kw"></span><strong>**kw</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.sessionmaker.params.**kw">¶</a> – all other keyword arguments are passed to the
constructor of newly created <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> objects.</p></li>
</ul>
</dd>
</dl>
</dd></dl>

<dl class="attribute">
<dt id="sqlalchemy.orm.session.sessionmaker.__le__">
<code class="descname">__le__</code><a class="headerlink" href="#sqlalchemy.orm.session.sessionmaker.__le__" title="Permalink to this definition">¶</a></dt>
<dd><div class="inherited-member docutils container">
<p><em>inherited from the</em> <code class="xref py py-attr docutils literal notranslate"><span class="pre">__le__</span></code> <em>attribute of</em> <code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></p>
</div>
<p>Return self&lt;=value.</p>
</dd></dl>

<dl class="attribute">
<dt id="sqlalchemy.orm.session.sessionmaker.__lt__">
<code class="descname">__lt__</code><a class="headerlink" href="#sqlalchemy.orm.session.sessionmaker.__lt__" title="Permalink to this definition">¶</a></dt>
<dd><div class="inherited-member docutils container">
<p><em>inherited from the</em> <code class="xref py py-attr docutils literal notranslate"><span class="pre">__lt__</span></code> <em>attribute of</em> <code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></p>
</div>
<p>Return self&lt;value.</p>
</dd></dl>

<dl class="attribute">
<dt id="sqlalchemy.orm.session.sessionmaker.__ne__">
<code class="descname">__ne__</code><a class="headerlink" href="#sqlalchemy.orm.session.sessionmaker.__ne__" title="Permalink to this definition">¶</a></dt>
<dd><div class="inherited-member docutils container">
<p><em>inherited from the</em> <code class="xref py py-attr docutils literal notranslate"><span class="pre">__ne__</span></code> <em>attribute of</em> <code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></p>
</div>
<p>Return self!=value.</p>
</dd></dl>

<dl class="classmethod">
<dt id="sqlalchemy.orm.session.sessionmaker.close_all">
<em class="property">classmethod </em><code class="descname">close_all</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.sessionmaker.close_all" title="Permalink to this definition">¶</a></dt>
<dd><div class="inherited-member docutils container">
<p><em>inherited from the</em> <code class="xref py py-meth docutils literal notranslate"><span class="pre">close_all()</span></code> <em>method of</em> <code class="xref py py-class docutils literal notranslate"><span class="pre">_SessionClassMethods</span></code></p>
</div>
<p>Close <em>all</em> sessions in memory.</p>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.orm.session.sessionmaker.configure">
<code class="descname">configure</code><span class="sig-paren">(</span><em>**new_kw</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.sessionmaker.configure" title="Permalink to this definition">¶</a></dt>
<dd><p>(Re)configure the arguments for this sessionmaker.</p>
<p>e.g.:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Session</span> <span class="o">=</span> <span class="n">sessionmaker</span><span class="p">()</span>

<span class="n">Session</span><span class="o">.</span><span class="n">configure</span><span class="p">(</span><span class="n">bind</span><span class="o">=</span><span class="n">create_engine</span><span class="p">(</span><span class="s1">&#39;sqlite://&#39;</span><span class="p">))</span></pre></div>
</div>
</dd></dl>

<dl class="classmethod">
<dt id="sqlalchemy.orm.session.sessionmaker.identity_key">
<em class="property">classmethod </em><code class="descname">identity_key</code><span class="sig-paren">(</span><em>orm_util</em>, <em>*args</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.sessionmaker.identity_key" title="Permalink to this definition">¶</a></dt>
<dd><div class="inherited-member docutils container">
<p><em>inherited from the</em> <code class="xref py py-meth docutils literal notranslate"><span class="pre">identity_key()</span></code> <em>method of</em> <code class="xref py py-class docutils literal notranslate"><span class="pre">_SessionClassMethods</span></code></p>
</div>
<p>Return an identity key.</p>
<p>This is an alias of <a class="reference internal" href="mapping_api.html#sqlalchemy.orm.util.identity_key" title="sqlalchemy.orm.util.identity_key"><code class="xref py py-func docutils literal notranslate"><span class="pre">util.identity_key()</span></code></a>.</p>
</dd></dl>

<dl class="classmethod">
<dt id="sqlalchemy.orm.session.sessionmaker.object_session">
<em class="property">classmethod </em><code class="descname">object_session</code><span class="sig-paren">(</span><em>instance</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.sessionmaker.object_session" title="Permalink to this definition">¶</a></dt>
<dd><div class="inherited-member docutils container">
<p><em>inherited from the</em> <code class="xref py py-meth docutils literal notranslate"><span class="pre">object_session()</span></code> <em>method of</em> <code class="xref py py-class docutils literal notranslate"><span class="pre">_SessionClassMethods</span></code></p>
</div>
<p>Return the <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> to which an object belongs.</p>
<p>This is an alias of <a class="reference internal" href="#sqlalchemy.orm.session.object_session" title="sqlalchemy.orm.session.object_session"><code class="xref py py-func docutils literal notranslate"><span class="pre">object_session()</span></code></a>.</p>
</dd></dl>

</dd></dl>

<dl class="class">
<dt id="sqlalchemy.orm.session.Session">
<em class="property">class </em><code class="descclassname">sqlalchemy.orm.session.</code><code class="descname">Session</code><span class="sig-paren">(</span><em>bind=None</em>, <em>autoflush=True</em>, <em>expire_on_commit=True</em>, <em>_enable_transaction_accounting=True</em>, <em>autocommit=False</em>, <em>twophase=False</em>, <em>weak_identity_map=True</em>, <em>binds=None</em>, <em>extension=None</em>, <em>enable_baked_queries=True</em>, <em>info=None</em>, <em>query_cls=&lt;class 'sqlalchemy.orm.query.Query'&gt;</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.Session" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <code class="xref py py-class docutils literal notranslate"><span class="pre">sqlalchemy.orm.session._SessionClassMethods</span></code></p>
<p>Manages persistence operations for ORM-mapped objects.</p>
<p>The Session’s usage paradigm is described at <a class="reference internal" href="session.html"><span class="doc">Using the Session</span></a>.</p>
<dl class="attribute">
<dt id="sqlalchemy.orm.session.Session.__eq__">
<code class="descname">__eq__</code><a class="headerlink" href="#sqlalchemy.orm.session.Session.__eq__" title="Permalink to this definition">¶</a></dt>
<dd><div class="inherited-member docutils container">
<p><em>inherited from the</em> <code class="xref py py-attr docutils literal notranslate"><span class="pre">__eq__</span></code> <em>attribute of</em> <code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></p>
</div>
<p>Return self==value.</p>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.orm.session.Session.__init__">
<code class="descname">__init__</code><span class="sig-paren">(</span><em>bind=None</em>, <em>autoflush=True</em>, <em>expire_on_commit=True</em>, <em>_enable_transaction_accounting=True</em>, <em>autocommit=False</em>, <em>twophase=False</em>, <em>weak_identity_map=True</em>, <em>binds=None</em>, <em>extension=None</em>, <em>enable_baked_queries=True</em>, <em>info=None</em>, <em>query_cls=&lt;class 'sqlalchemy.orm.query.Query'&gt;</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.Session.__init__" title="Permalink to this definition">¶</a></dt>
<dd><p>Construct a new Session.</p>
<p>See also the <a class="reference internal" href="#sqlalchemy.orm.session.sessionmaker" title="sqlalchemy.orm.session.sessionmaker"><code class="xref py py-class docutils literal notranslate"><span class="pre">sessionmaker</span></code></a> function which is used to
generate a <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a>-producing callable with a given
set of arguments.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><span class="target" id="sqlalchemy.orm.session.Session.params.autocommit"></span><strong>autocommit</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.params.autocommit">¶</a> – <div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>The autocommit flag is <strong>not for general use</strong>, and if it is
used, queries should only be invoked within the span of a
<a class="reference internal" href="#sqlalchemy.orm.session.Session.begin" title="sqlalchemy.orm.session.Session.begin"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.begin()</span></code></a> / <a class="reference internal" href="#sqlalchemy.orm.session.Session.commit" title="sqlalchemy.orm.session.Session.commit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.commit()</span></code></a> pair.  Executing
queries outside of a demarcated transaction is a legacy mode
of usage, and can in some cases lead to concurrent connection
checkouts.</p>
</div>
<p>Defaults to <code class="docutils literal notranslate"><span class="pre">False</span></code>. When <code class="docutils literal notranslate"><span class="pre">True</span></code>, the
<a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> does not keep a persistent transaction running,
and will acquire connections from the engine on an as-needed basis,
returning them immediately after their use. Flushes will begin and
commit (or possibly rollback) their own transaction if no
transaction is present. When using this mode, the
<a class="reference internal" href="#sqlalchemy.orm.session.Session.begin" title="sqlalchemy.orm.session.Session.begin"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.begin()</span></code></a> method is used to explicitly start
transactions.</p>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="session_transaction.html#session-autocommit"><span class="std std-ref">Autocommit Mode</span></a></p>
</div>
</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.Session.params.autoflush"></span><strong>autoflush</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.params.autoflush">¶</a> – When <code class="docutils literal notranslate"><span class="pre">True</span></code>, all query operations will issue a
<a class="reference internal" href="#sqlalchemy.orm.session.Session.flush" title="sqlalchemy.orm.session.Session.flush"><code class="xref py py-meth docutils literal notranslate"><span class="pre">flush()</span></code></a> call to this <code class="docutils literal notranslate"><span class="pre">Session</span></code> before proceeding.
This is a convenience feature so that <a class="reference internal" href="#sqlalchemy.orm.session.Session.flush" title="sqlalchemy.orm.session.Session.flush"><code class="xref py py-meth docutils literal notranslate"><span class="pre">flush()</span></code></a> need
not be called repeatedly in order for database queries to retrieve
results. It’s typical that <code class="docutils literal notranslate"><span class="pre">autoflush</span></code> is used in conjunction
with <code class="docutils literal notranslate"><span class="pre">autocommit=False</span></code>. In this scenario, explicit calls to
<a class="reference internal" href="#sqlalchemy.orm.session.Session.flush" title="sqlalchemy.orm.session.Session.flush"><code class="xref py py-meth docutils literal notranslate"><span class="pre">flush()</span></code></a> are rarely needed; you usually only need to
call <a class="reference internal" href="#sqlalchemy.orm.session.Session.commit" title="sqlalchemy.orm.session.Session.commit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">commit()</span></code></a> (which flushes) to finalize changes.</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.Session.params.bind"></span><strong>bind</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.params.bind">¶</a> – An optional <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Engine</span></code></a> or <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Connection" title="sqlalchemy.engine.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a> to
which this <code class="docutils literal notranslate"><span class="pre">Session</span></code> should be bound. When specified, all SQL
operations performed by this session will execute via this
connectable.</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.Session.params.binds"></span><strong>binds</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.params.binds">¶</a> – <p>A dictionary which may specify any number of
<a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Engine</span></code></a> or <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Connection" title="sqlalchemy.engine.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a> objects as the source of
connectivity for SQL operations on a per-entity basis.   The keys
of the dictionary consist of any series of mapped classes,
arbitrary Python classes that are bases for mapped classes,
<a class="reference internal" href="../core/metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><code class="xref py py-class docutils literal notranslate"><span class="pre">Table</span></code></a> objects and <a class="reference internal" href="mapping_api.html#sqlalchemy.orm.mapper.Mapper" title="sqlalchemy.orm.mapper.Mapper"><code class="xref py py-class docutils literal notranslate"><span class="pre">Mapper</span></code></a> objects.  The
values of the dictionary are then instances of <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Engine</span></code></a>
or less commonly <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Connection" title="sqlalchemy.engine.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a> objects.  Operations which
proceed relative to a particular mapped class will consult this
dictionary for the closest matching entity in order to determine
which <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Engine</span></code></a> should be used for a particular SQL
operation.    The complete heuristics for resolution are
described at <a class="reference internal" href="#sqlalchemy.orm.session.Session.get_bind" title="sqlalchemy.orm.session.Session.get_bind"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.get_bind()</span></code></a>.  Usage looks like:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Session</span> <span class="o">=</span> <span class="n">sessionmaker</span><span class="p">(</span><span class="n">binds</span><span class="o">=</span><span class="p">{</span>
    <span class="n">SomeMappedClass</span><span class="p">:</span> <span class="n">create_engine</span><span class="p">(</span><span class="s1">&#39;postgresql://engine1&#39;</span><span class="p">),</span>
    <span class="n">SomeDeclarativeBase</span><span class="p">:</span> <span class="n">create_engine</span><span class="p">(</span><span class="s1">&#39;postgresql://engine2&#39;</span><span class="p">),</span>
    <span class="n">some_mapper</span><span class="p">:</span> <span class="n">create_engine</span><span class="p">(</span><span class="s1">&#39;postgresql://engine3&#39;</span><span class="p">),</span>
    <span class="n">some_table</span><span class="p">:</span> <span class="n">create_engine</span><span class="p">(</span><span class="s1">&#39;postgresql://engine4&#39;</span><span class="p">),</span>
    <span class="p">})</span></pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="persistence_techniques.html#session-partitioning"><span class="std std-ref">Partitioning Strategies (e.g. multiple database backends per Session)</span></a></p>
<p><a class="reference internal" href="#sqlalchemy.orm.session.Session.bind_mapper" title="sqlalchemy.orm.session.Session.bind_mapper"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.bind_mapper()</span></code></a></p>
<p><a class="reference internal" href="#sqlalchemy.orm.session.Session.bind_table" title="sqlalchemy.orm.session.Session.bind_table"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.bind_table()</span></code></a></p>
<p><a class="reference internal" href="#sqlalchemy.orm.session.Session.get_bind" title="sqlalchemy.orm.session.Session.get_bind"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.get_bind()</span></code></a></p>
</div>
</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.Session.params.class_"></span><strong>class_</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.params.class_">¶</a> – Specify an alternate class other than
<code class="docutils literal notranslate"><span class="pre">sqlalchemy.orm.session.Session</span></code> which should be used by the
returned class. This is the only argument that is local to the
<a class="reference internal" href="#sqlalchemy.orm.session.sessionmaker" title="sqlalchemy.orm.session.sessionmaker"><code class="xref py py-class docutils literal notranslate"><span class="pre">sessionmaker</span></code></a> function, and is not sent directly to the
constructor for <code class="docutils literal notranslate"><span class="pre">Session</span></code>.</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.Session.params.enable_baked_queries"></span><strong>enable_baked_queries</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.params.enable_baked_queries">¶</a> – <p>defaults to <code class="docutils literal notranslate"><span class="pre">True</span></code>.  A flag consumed
by the <a class="reference internal" href="extensions/baked.html#module-sqlalchemy.ext.baked" title="sqlalchemy.ext.baked"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlalchemy.ext.baked</span></code></a> extension to determine if
“baked queries” should be cached, as is the normal operation
of this extension.  When set to <code class="docutils literal notranslate"><span class="pre">False</span></code>, all caching is disabled,
including baked queries defined by the calling application as
well as those used internally.  Setting this flag to <code class="docutils literal notranslate"><span class="pre">False</span></code>
can significantly reduce memory use, however will also degrade
performance for those areas that make use of baked queries
(such as relationship loaders).   Additionally, baked query
logic in the calling application or potentially within the ORM
that may be malfunctioning due to cache key collisions or similar
can be flagged by observing if this flag resolves the issue.</p>
<div class="versionadded">
<p><span class="versionmodified added">New in version 1.2.</span></p>
</div>
</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.Session.params._enable_transaction_accounting"></span><strong>_enable_transaction_accounting</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.params._enable_transaction_accounting">¶</a> – <p>Defaults to <code class="docutils literal notranslate"><span class="pre">True</span></code>.  A
legacy-only flag which when <code class="docutils literal notranslate"><span class="pre">False</span></code> disables <em>all</em> 0.5-style
object accounting on transaction boundaries.</p>
<div class="deprecated">
<p><span class="versionmodified deprecated">Deprecated since version 0.7: </span>the <a class="reference internal" href="#sqlalchemy.orm.session.Session.params._enable_transaction_accounting" title="sqlalchemy.orm.session.Session"><code class="xref py py-paramref docutils literal notranslate"><span class="pre">Session._enable_transaction_accounting</span></code></a>
parameter will be removed in a future release.</p>
</div>
</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.Session.params.expire_on_commit"></span><strong>expire_on_commit</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.params.expire_on_commit">¶</a> – Defaults to <code class="docutils literal notranslate"><span class="pre">True</span></code>. When <code class="docutils literal notranslate"><span class="pre">True</span></code>, all
instances will be fully expired after each <a class="reference internal" href="#sqlalchemy.orm.session.Session.commit" title="sqlalchemy.orm.session.Session.commit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">commit()</span></code></a>,
so that all attribute/object access subsequent to a completed
transaction will load from the most recent database state.</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.Session.params.extension"></span><strong>extension</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.params.extension">¶</a> – <p>An optional
<a class="reference internal" href="deprecated.html#sqlalchemy.orm.interfaces.SessionExtension" title="sqlalchemy.orm.interfaces.SessionExtension"><code class="xref py py-class docutils literal notranslate"><span class="pre">SessionExtension</span></code></a> instance, or a list
of such instances, which will receive pre- and post- commit and
flush events, as well as a post-rollback event.</p>
<div class="deprecated">
<p><span class="versionmodified deprecated">Deprecated since version 0.7: </span><a class="reference internal" href="deprecated.html#sqlalchemy.orm.interfaces.SessionExtension" title="sqlalchemy.orm.interfaces.SessionExtension"><code class="xref py py-class docutils literal notranslate"><span class="pre">SessionExtension</span></code></a> is deprecated in favor of the
<a class="reference internal" href="events.html#sqlalchemy.orm.events.SessionEvents" title="sqlalchemy.orm.events.SessionEvents"><code class="xref py py-class docutils literal notranslate"><span class="pre">SessionEvents</span></code></a> listener interface.  The
<a class="reference internal" href="#sqlalchemy.orm.session.Session.params.extension" title="sqlalchemy.orm.session.Session"><code class="xref py py-paramref docutils literal notranslate"><span class="pre">Session.extension</span></code></a> parameter will be
removed in a future release.</p>
</div>
</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.Session.params.info"></span><strong>info</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.params.info">¶</a> – <p>optional dictionary of arbitrary data to be associated
with this <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a>.  Is available via the
<a class="reference internal" href="#sqlalchemy.orm.session.Session.info" title="sqlalchemy.orm.session.Session.info"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Session.info</span></code></a> attribute.  Note the dictionary is copied at
construction time so that modifications to the per-
<a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> dictionary will be local to that
<a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a>.</p>
<div class="versionadded">
<p><span class="versionmodified added">New in version 0.9.0.</span></p>
</div>
</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.Session.params.query_cls"></span><strong>query_cls</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.params.query_cls">¶</a> – Class which should be used to create new Query
objects, as returned by the <a class="reference internal" href="#sqlalchemy.orm.session.Session.query" title="sqlalchemy.orm.session.Session.query"><code class="xref py py-meth docutils literal notranslate"><span class="pre">query()</span></code></a> method.
Defaults to <a class="reference internal" href="query.html#sqlalchemy.orm.query.Query" title="sqlalchemy.orm.query.Query"><code class="xref py py-class docutils literal notranslate"><span class="pre">Query</span></code></a>.</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.Session.params.twophase"></span><strong>twophase</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.params.twophase">¶</a> – When <code class="docutils literal notranslate"><span class="pre">True</span></code>, all transactions will be started as
a “two phase” transaction, i.e. using the “two phase” semantics
of the database in use along with an XID.  During a
<a class="reference internal" href="#sqlalchemy.orm.session.Session.commit" title="sqlalchemy.orm.session.Session.commit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">commit()</span></code></a>, after <a class="reference internal" href="#sqlalchemy.orm.session.Session.flush" title="sqlalchemy.orm.session.Session.flush"><code class="xref py py-meth docutils literal notranslate"><span class="pre">flush()</span></code></a> has been issued for all
attached databases, the <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.TwoPhaseTransaction.prepare" title="sqlalchemy.engine.TwoPhaseTransaction.prepare"><code class="xref py py-meth docutils literal notranslate"><span class="pre">prepare()</span></code></a>
method on each database’s <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.TwoPhaseTransaction" title="sqlalchemy.engine.TwoPhaseTransaction"><code class="xref py py-class docutils literal notranslate"><span class="pre">TwoPhaseTransaction</span></code></a> will be
called. This allows each database to roll back the entire
transaction, before each transaction is committed.</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.Session.params.weak_identity_map"></span><strong>weak_identity_map</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.params.weak_identity_map">¶</a> – <p>Defaults to <code class="docutils literal notranslate"><span class="pre">True</span></code> - when set to
<code class="docutils literal notranslate"><span class="pre">False</span></code>, objects placed in the <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> will be
strongly referenced until explicitly removed or the
<a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> is closed.</p>
<div class="deprecated">
<p><span class="versionmodified deprecated">Deprecated since version 1.0: </span>The <a class="reference internal" href="#sqlalchemy.orm.session.Session.params.weak_identity_map" title="sqlalchemy.orm.session.Session"><code class="xref py py-paramref docutils literal notranslate"><span class="pre">Session.weak_identity_map</span></code></a> parameter as well as
the strong-referencing identity map are deprecated, and will be
removed in a future release.  For the use case where objects
present in a <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> need to be automatically strong
referenced, see the recipe at
<a class="reference internal" href="session_state_management.html#session-referencing-behavior"><span class="std std-ref">Session Referencing Behavior</span></a> for an event-based approach
to maintaining strong identity references.</p>
</div>
</p></li>
</ul>
</dd>
</dl>
</dd></dl>

<dl class="attribute">
<dt id="sqlalchemy.orm.session.Session.__le__">
<code class="descname">__le__</code><a class="headerlink" href="#sqlalchemy.orm.session.Session.__le__" title="Permalink to this definition">¶</a></dt>
<dd><div class="inherited-member docutils container">
<p><em>inherited from the</em> <code class="xref py py-attr docutils literal notranslate"><span class="pre">__le__</span></code> <em>attribute of</em> <code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></p>
</div>
<p>Return self&lt;=value.</p>
</dd></dl>

<dl class="attribute">
<dt id="sqlalchemy.orm.session.Session.__lt__">
<code class="descname">__lt__</code><a class="headerlink" href="#sqlalchemy.orm.session.Session.__lt__" title="Permalink to this definition">¶</a></dt>
<dd><div class="inherited-member docutils container">
<p><em>inherited from the</em> <code class="xref py py-attr docutils literal notranslate"><span class="pre">__lt__</span></code> <em>attribute of</em> <code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></p>
</div>
<p>Return self&lt;value.</p>
</dd></dl>

<dl class="attribute">
<dt id="sqlalchemy.orm.session.Session.__ne__">
<code class="descname">__ne__</code><a class="headerlink" href="#sqlalchemy.orm.session.Session.__ne__" title="Permalink to this definition">¶</a></dt>
<dd><div class="inherited-member docutils container">
<p><em>inherited from the</em> <code class="xref py py-attr docutils literal notranslate"><span class="pre">__ne__</span></code> <em>attribute of</em> <code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></p>
</div>
<p>Return self!=value.</p>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.orm.session.Session.add">
<code class="descname">add</code><span class="sig-paren">(</span><em>instance</em>, <em>_warn=True</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.Session.add" title="Permalink to this definition">¶</a></dt>
<dd><p>Place an object in the <code class="docutils literal notranslate"><span class="pre">Session</span></code>.</p>
<p>Its state will be persisted to the database on the next flush
operation.</p>
<p>Repeated calls to <code class="docutils literal notranslate"><span class="pre">add()</span></code> will be ignored. The opposite of <code class="docutils literal notranslate"><span class="pre">add()</span></code>
is <code class="docutils literal notranslate"><span class="pre">expunge()</span></code>.</p>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.orm.session.Session.add_all">
<code class="descname">add_all</code><span class="sig-paren">(</span><em>instances</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.Session.add_all" title="Permalink to this definition">¶</a></dt>
<dd><p>Add the given collection of instances to this <code class="docutils literal notranslate"><span class="pre">Session</span></code>.</p>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.orm.session.Session.begin">
<code class="descname">begin</code><span class="sig-paren">(</span><em>subtransactions=False</em>, <em>nested=False</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.Session.begin" title="Permalink to this definition">¶</a></dt>
<dd><p>Begin a transaction on this <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a>.</p>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>The <a class="reference internal" href="#sqlalchemy.orm.session.Session.begin" title="sqlalchemy.orm.session.Session.begin"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.begin()</span></code></a> method is part of a larger pattern
of use with the <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> known as <strong>autocommit mode</strong>.
This is essentially a <strong>legacy mode of use</strong> and is
not necessary for new applications.    The <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a>
normally handles the work of “begin” transparently, which in
turn relies upon the Python DBAPI to transparently “begin”
transactions; there is <strong>no need to explicitly begin transactions</strong>
when using modern <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> programming patterns.
In its default mode of <code class="docutils literal notranslate"><span class="pre">autocommit=False</span></code>, the
<a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> does all of its work within
the context of a transaction, so as soon as you call
<a class="reference internal" href="#sqlalchemy.orm.session.Session.commit" title="sqlalchemy.orm.session.Session.commit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.commit()</span></code></a>, the next transaction is implicitly
started when the next database operation is invoked.  See
<a class="reference internal" href="session_transaction.html#session-autocommit"><span class="std std-ref">Autocommit Mode</span></a> for further background.</p>
</div>
<p>The method will raise an error if this <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> is already
inside of a transaction, unless
<a class="reference internal" href="#sqlalchemy.orm.session.Session.begin.params.subtransactions" title="sqlalchemy.orm.session.Session.begin"><code class="xref py py-paramref docutils literal notranslate"><span class="pre">subtransactions</span></code></a> or
<a class="reference internal" href="#sqlalchemy.orm.session.Session.begin.params.nested" title="sqlalchemy.orm.session.Session.begin"><code class="xref py py-paramref docutils literal notranslate"><span class="pre">nested</span></code></a> are specified.  A “subtransaction”
is essentially a code embedding pattern that does not affect the
transactional state of the database connection unless a rollback is
emitted, in which case the whole transaction is rolled back.  For
documentation on subtransactions, please see
<a class="reference internal" href="session_transaction.html#session-subtransactions"><span class="std std-ref">Using Subtransactions with Autocommit</span></a>.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><span class="target" id="sqlalchemy.orm.session.Session.begin.params.subtransactions"></span><strong>subtransactions</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.begin.params.subtransactions">¶</a> – if True, indicates that this
<a class="reference internal" href="#sqlalchemy.orm.session.Session.begin" title="sqlalchemy.orm.session.Session.begin"><code class="xref py py-meth docutils literal notranslate"><span class="pre">begin()</span></code></a> can create a “subtransaction”.</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.Session.begin.params.nested"></span><strong>nested</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.begin.params.nested">¶</a> – if True, begins a SAVEPOINT transaction and is
equivalent to calling <a class="reference internal" href="#sqlalchemy.orm.session.Session.begin_nested" title="sqlalchemy.orm.session.Session.begin_nested"><code class="xref py py-meth docutils literal notranslate"><span class="pre">begin_nested()</span></code></a>. For
documentation on SAVEPOINT transactions, please see
<a class="reference internal" href="session_transaction.html#session-begin-nested"><span class="std std-ref">Using SAVEPOINT</span></a>.</p></li>
</ul>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p>the <a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction" title="sqlalchemy.orm.session.SessionTransaction"><code class="xref py py-class docutils literal notranslate"><span class="pre">SessionTransaction</span></code></a> object.  Note that
<a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction" title="sqlalchemy.orm.session.SessionTransaction"><code class="xref py py-class docutils literal notranslate"><span class="pre">SessionTransaction</span></code></a>
acts as a Python context manager, allowing <a class="reference internal" href="#sqlalchemy.orm.session.Session.begin" title="sqlalchemy.orm.session.Session.begin"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.begin()</span></code></a>
to be used in a “with” block.  See <a class="reference internal" href="session_transaction.html#session-autocommit"><span class="std std-ref">Autocommit Mode</span></a> for
an example.</p>
</dd>
</dl>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="session_transaction.html#session-autocommit"><span class="std std-ref">Autocommit Mode</span></a></p>
<p><a class="reference internal" href="#sqlalchemy.orm.session.Session.begin_nested" title="sqlalchemy.orm.session.Session.begin_nested"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.begin_nested()</span></code></a></p>
</div>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.orm.session.Session.begin_nested">
<code class="descname">begin_nested</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.Session.begin_nested" title="Permalink to this definition">¶</a></dt>
<dd><p>Begin a “nested” transaction on this Session, e.g. SAVEPOINT.</p>
<p>The target database(s) and associated drivers must support SQL
SAVEPOINT for this method to function correctly.</p>
<p>For documentation on SAVEPOINT
transactions, please see <a class="reference internal" href="session_transaction.html#session-begin-nested"><span class="std std-ref">Using SAVEPOINT</span></a>.</p>
<dl class="field-list simple">
<dt class="field-odd">Returns</dt>
<dd class="field-odd"><p>the <a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction" title="sqlalchemy.orm.session.SessionTransaction"><code class="xref py py-class docutils literal notranslate"><span class="pre">SessionTransaction</span></code></a> object.  Note that
<a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction" title="sqlalchemy.orm.session.SessionTransaction"><code class="xref py py-class docutils literal notranslate"><span class="pre">SessionTransaction</span></code></a> acts as a context manager, allowing
<a class="reference internal" href="#sqlalchemy.orm.session.Session.begin_nested" title="sqlalchemy.orm.session.Session.begin_nested"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.begin_nested()</span></code></a> to be used in a “with” block.
See <a class="reference internal" href="session_transaction.html#session-begin-nested"><span class="std std-ref">Using SAVEPOINT</span></a> for a usage example.</p>
</dd>
</dl>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="session_transaction.html#session-begin-nested"><span class="std std-ref">Using SAVEPOINT</span></a></p>
<p><a class="reference internal" href="../dialects/sqlite.html#pysqlite-serializable"><span class="std std-ref">Serializable isolation / Savepoints / Transactional DDL</span></a> - special workarounds required
with the SQLite driver in order for SAVEPOINT to work
correctly.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.orm.session.Session.bind_mapper">
<code class="descname">bind_mapper</code><span class="sig-paren">(</span><em>mapper</em>, <em>bind</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.Session.bind_mapper" title="Permalink to this definition">¶</a></dt>
<dd><p>Associate a <a class="reference internal" href="mapping_api.html#sqlalchemy.orm.mapper.Mapper" title="sqlalchemy.orm.mapper.Mapper"><code class="xref py py-class docutils literal notranslate"><span class="pre">Mapper</span></code></a> or arbitrary Python class with a
“bind”, e.g. an <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Engine</span></code></a> or <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Connection" title="sqlalchemy.engine.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a>.</p>
<p>The given entity is added to a lookup used by the
<a class="reference internal" href="#sqlalchemy.orm.session.Session.get_bind" title="sqlalchemy.orm.session.Session.get_bind"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.get_bind()</span></code></a> method.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><span class="target" id="sqlalchemy.orm.session.Session.bind_mapper.params.mapper"></span><strong>mapper</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.bind_mapper.params.mapper">¶</a> – a <a class="reference internal" href="mapping_api.html#sqlalchemy.orm.mapper.Mapper" title="sqlalchemy.orm.mapper.Mapper"><code class="xref py py-class docutils literal notranslate"><span class="pre">Mapper</span></code></a> object, or an instance of a mapped
class, or any Python class that is the base of a set of mapped
classes.</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.Session.bind_mapper.params.bind"></span><strong>bind</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.bind_mapper.params.bind">¶</a> – an <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Engine</span></code></a> or <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Connection" title="sqlalchemy.engine.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a> object.</p></li>
</ul>
</dd>
</dl>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="persistence_techniques.html#session-partitioning"><span class="std std-ref">Partitioning Strategies (e.g. multiple database backends per Session)</span></a></p>
<p><a class="reference internal" href="#sqlalchemy.orm.session.Session.params.binds" title="sqlalchemy.orm.session.Session"><code class="xref py py-paramref docutils literal notranslate"><span class="pre">Session.binds</span></code></a></p>
<p><a class="reference internal" href="#sqlalchemy.orm.session.Session.bind_table" title="sqlalchemy.orm.session.Session.bind_table"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.bind_table()</span></code></a></p>
</div>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.orm.session.Session.bind_table">
<code class="descname">bind_table</code><span class="sig-paren">(</span><em>table</em>, <em>bind</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.Session.bind_table" title="Permalink to this definition">¶</a></dt>
<dd><p>Associate a <a class="reference internal" href="../core/metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><code class="xref py py-class docutils literal notranslate"><span class="pre">Table</span></code></a> with a “bind”, e.g. an <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Engine</span></code></a>
or <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Connection" title="sqlalchemy.engine.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a>.</p>
<p>The given <a class="reference internal" href="../core/metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><code class="xref py py-class docutils literal notranslate"><span class="pre">Table</span></code></a> is added to a lookup used by the
<a class="reference internal" href="#sqlalchemy.orm.session.Session.get_bind" title="sqlalchemy.orm.session.Session.get_bind"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.get_bind()</span></code></a> method.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><span class="target" id="sqlalchemy.orm.session.Session.bind_table.params.table"></span><strong>table</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.bind_table.params.table">¶</a> – a <a class="reference internal" href="../core/metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><code class="xref py py-class docutils literal notranslate"><span class="pre">Table</span></code></a> object, which is typically the target
of an ORM mapping, or is present within a selectable that is
mapped.</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.Session.bind_table.params.bind"></span><strong>bind</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.bind_table.params.bind">¶</a> – an <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Engine</span></code></a> or <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Connection" title="sqlalchemy.engine.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a> object.</p></li>
</ul>
</dd>
</dl>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="persistence_techniques.html#session-partitioning"><span class="std std-ref">Partitioning Strategies (e.g. multiple database backends per Session)</span></a></p>
<p><a class="reference internal" href="#sqlalchemy.orm.session.Session.params.binds" title="sqlalchemy.orm.session.Session"><code class="xref py py-paramref docutils literal notranslate"><span class="pre">Session.binds</span></code></a></p>
<p><a class="reference internal" href="#sqlalchemy.orm.session.Session.bind_mapper" title="sqlalchemy.orm.session.Session.bind_mapper"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.bind_mapper()</span></code></a></p>
</div>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.orm.session.Session.bulk_insert_mappings">
<code class="descname">bulk_insert_mappings</code><span class="sig-paren">(</span><em>mapper</em>, <em>mappings</em>, <em>return_defaults=False</em>, <em>render_nulls=False</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.Session.bulk_insert_mappings" title="Permalink to this definition">¶</a></dt>
<dd><p>Perform a bulk insert of the given list of mapping dictionaries.</p>
<p>The bulk insert feature allows plain Python dictionaries to be used as
the source of simple INSERT operations which can be more easily
grouped together into higher performing “executemany”
operations.  Using dictionaries, there is no “history” or session
state management features in use, reducing latency when inserting
large numbers of simple rows.</p>
<p>The values within the dictionaries as given are typically passed
without modification into Core <code class="xref py py-meth docutils literal notranslate"><span class="pre">Insert()</span></code> constructs, after
organizing the values within them across the tables to which
the given mapper is mapped.</p>
<div class="versionadded">
<p><span class="versionmodified added">New in version 1.0.0.</span></p>
</div>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>The bulk insert feature allows for a lower-latency INSERT
of rows at the expense of most other unit-of-work features.
Features such as object management, relationship handling,
and SQL clause support are <strong>silently omitted</strong> in favor of raw
INSERT of records.</p>
<p><strong>Please read the list of caveats at</strong> <a class="reference internal" href="persistence_techniques.html#bulk-operations"><span class="std std-ref">Bulk Operations</span></a>
<strong>before using this method, and fully test and confirm the
functionality of all code developed using these systems.</strong></p>
</div>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><span class="target" id="sqlalchemy.orm.session.Session.bulk_insert_mappings.params.mapper"></span><strong>mapper</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.bulk_insert_mappings.params.mapper">¶</a> – a mapped class, or the actual <a class="reference internal" href="mapping_api.html#sqlalchemy.orm.mapper.Mapper" title="sqlalchemy.orm.mapper.Mapper"><code class="xref py py-class docutils literal notranslate"><span class="pre">Mapper</span></code></a> object,
representing the single kind of object represented within the mapping
list.</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.Session.bulk_insert_mappings.params.mappings"></span><strong>mappings</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.bulk_insert_mappings.params.mappings">¶</a> – a list of dictionaries, each one containing the state
of the mapped row to be inserted, in terms of the attribute names
on the mapped class.   If the mapping refers to multiple tables,
such as a joined-inheritance mapping, each dictionary must contain
all keys to be populated into all tables.</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.Session.bulk_insert_mappings.params.return_defaults"></span><strong>return_defaults</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.bulk_insert_mappings.params.return_defaults">¶</a> – when True, rows that are missing values which
generate defaults, namely integer primary key defaults and sequences,
will be inserted <strong>one at a time</strong>, so that the primary key value
is available.  In particular this will allow joined-inheritance
and other multi-table mappings to insert correctly without the need
to provide primary
key values ahead of time; however,
<a class="reference internal" href="#sqlalchemy.orm.session.Session.bulk_insert_mappings.params.return_defaults" title="sqlalchemy.orm.session.Session.bulk_insert_mappings"><code class="xref py py-paramref docutils literal notranslate"><span class="pre">Session.bulk_insert_mappings.return_defaults</span></code></a>
<strong>greatly reduces the performance gains</strong> of the method overall.
If the rows
to be inserted only refer to a single table, then there is no
reason this flag should be set as the returned default information
is not used.</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.Session.bulk_insert_mappings.params.render_nulls"></span><strong>render_nulls</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.bulk_insert_mappings.params.render_nulls">¶</a> – <p>When True, a value of <code class="docutils literal notranslate"><span class="pre">None</span></code> will result
in a NULL value being included in the INSERT statement, rather
than the column being omitted from the INSERT.   This allows all
the rows being INSERTed to have the identical set of columns which
allows the full set of rows to be batched to the DBAPI.  Normally,
each column-set that contains a different combination of NULL values
than the previous row must omit a different series of columns from
the rendered INSERT statement, which means it must be emitted as a
separate statement.   By passing this flag, the full set of rows
are guaranteed to be batchable into one batch; the cost however is
that server-side defaults which are invoked by an omitted column will
be skipped, so care must be taken to ensure that these are not
necessary.</p>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>When this flag is set, <strong>server side default SQL values will
not be invoked</strong> for those columns that are inserted as NULL;
the NULL value will be sent explicitly.   Care must be taken
to ensure that no server-side default functions need to be
invoked for the operation as a whole.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified added">New in version 1.1.</span></p>
</div>
</p></li>
</ul>
</dd>
</dl>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="persistence_techniques.html#bulk-operations"><span class="std std-ref">Bulk Operations</span></a></p>
<p><a class="reference internal" href="#sqlalchemy.orm.session.Session.bulk_save_objects" title="sqlalchemy.orm.session.Session.bulk_save_objects"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.bulk_save_objects()</span></code></a></p>
<p><a class="reference internal" href="#sqlalchemy.orm.session.Session.bulk_update_mappings" title="sqlalchemy.orm.session.Session.bulk_update_mappings"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.bulk_update_mappings()</span></code></a></p>
</div>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.orm.session.Session.bulk_save_objects">
<code class="descname">bulk_save_objects</code><span class="sig-paren">(</span><em>objects</em>, <em>return_defaults=False</em>, <em>update_changed_only=True</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.Session.bulk_save_objects" title="Permalink to this definition">¶</a></dt>
<dd><p>Perform a bulk save of the given list of objects.</p>
<p>The bulk save feature allows mapped objects to be used as the
source of simple INSERT and UPDATE operations which can be more easily
grouped together into higher performing “executemany”
operations; the extraction of data from the objects is also performed
using a lower-latency process that ignores whether or not attributes
have actually been modified in the case of UPDATEs, and also ignores
SQL expressions.</p>
<p>The objects as given are not added to the session and no additional
state is established on them, unless the <code class="docutils literal notranslate"><span class="pre">return_defaults</span></code> flag
is also set, in which case primary key attributes and server-side
default values will be populated.</p>
<div class="versionadded">
<p><span class="versionmodified added">New in version 1.0.0.</span></p>
</div>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>The bulk save feature allows for a lower-latency INSERT/UPDATE
of rows at the expense of most other unit-of-work features.
Features such as object management, relationship handling,
and SQL clause support are <strong>silently omitted</strong> in favor of raw
INSERT/UPDATES of records.</p>
<p><strong>Please read the list of caveats at</strong> <a class="reference internal" href="persistence_techniques.html#bulk-operations"><span class="std std-ref">Bulk Operations</span></a>
<strong>before using this method, and fully test and confirm the
functionality of all code developed using these systems.</strong></p>
</div>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><span class="target" id="sqlalchemy.orm.session.Session.bulk_save_objects.params.objects"></span><strong>objects</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.bulk_save_objects.params.objects">¶</a> – <p>a list of mapped object instances.  The mapped
objects are persisted as is, and are <strong>not</strong> associated with the
<a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> afterwards.</p>
<p>For each object, whether the object is sent as an INSERT or an
UPDATE is dependent on the same rules used by the <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a>
in traditional operation; if the object has the
<code class="xref py py-attr docutils literal notranslate"><span class="pre">InstanceState.key</span></code>
attribute set, then the object is assumed to be “detached” and
will result in an UPDATE.  Otherwise, an INSERT is used.</p>
<p>In the case of an UPDATE, statements are grouped based on which
attributes have changed, and are thus to be the subject of each
SET clause.  If <code class="docutils literal notranslate"><span class="pre">update_changed_only</span></code> is False, then all
attributes present within each object are applied to the UPDATE
statement, which may help in allowing the statements to be grouped
together into a larger executemany(), and will also reduce the
overhead of checking history on attributes.</p>
</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.Session.bulk_save_objects.params.return_defaults"></span><strong>return_defaults</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.bulk_save_objects.params.return_defaults">¶</a> – when True, rows that are missing values which
generate defaults, namely integer primary key defaults and sequences,
will be inserted <strong>one at a time</strong>, so that the primary key value
is available.  In particular this will allow joined-inheritance
and other multi-table mappings to insert correctly without the need
to provide primary key values ahead of time; however,
<a class="reference internal" href="#sqlalchemy.orm.session.Session.bulk_save_objects.params.return_defaults" title="sqlalchemy.orm.session.Session.bulk_save_objects"><code class="xref py py-paramref docutils literal notranslate"><span class="pre">Session.bulk_save_objects.return_defaults</span></code></a> <strong>greatly
reduces the performance gains</strong> of the method overall.</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.Session.bulk_save_objects.params.update_changed_only"></span><strong>update_changed_only</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.bulk_save_objects.params.update_changed_only">¶</a> – when True, UPDATE statements are rendered
based on those attributes in each state that have logged changes.
When False, all attributes present are rendered into the SET clause
with the exception of primary key attributes.</p></li>
</ul>
</dd>
</dl>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="persistence_techniques.html#bulk-operations"><span class="std std-ref">Bulk Operations</span></a></p>
<p><a class="reference internal" href="#sqlalchemy.orm.session.Session.bulk_insert_mappings" title="sqlalchemy.orm.session.Session.bulk_insert_mappings"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.bulk_insert_mappings()</span></code></a></p>
<p><a class="reference internal" href="#sqlalchemy.orm.session.Session.bulk_update_mappings" title="sqlalchemy.orm.session.Session.bulk_update_mappings"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.bulk_update_mappings()</span></code></a></p>
</div>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.orm.session.Session.bulk_update_mappings">
<code class="descname">bulk_update_mappings</code><span class="sig-paren">(</span><em>mapper</em>, <em>mappings</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.Session.bulk_update_mappings" title="Permalink to this definition">¶</a></dt>
<dd><p>Perform a bulk update of the given list of mapping dictionaries.</p>
<p>The bulk update feature allows plain Python dictionaries to be used as
the source of simple UPDATE operations which can be more easily
grouped together into higher performing “executemany”
operations.  Using dictionaries, there is no “history” or session
state management features in use, reducing latency when updating
large numbers of simple rows.</p>
<div class="versionadded">
<p><span class="versionmodified added">New in version 1.0.0.</span></p>
</div>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>The bulk update feature allows for a lower-latency UPDATE
of rows at the expense of most other unit-of-work features.
Features such as object management, relationship handling,
and SQL clause support are <strong>silently omitted</strong> in favor of raw
UPDATES of records.</p>
<p><strong>Please read the list of caveats at</strong> <a class="reference internal" href="persistence_techniques.html#bulk-operations"><span class="std std-ref">Bulk Operations</span></a>
<strong>before using this method, and fully test and confirm the
functionality of all code developed using these systems.</strong></p>
</div>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><span class="target" id="sqlalchemy.orm.session.Session.bulk_update_mappings.params.mapper"></span><strong>mapper</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.bulk_update_mappings.params.mapper">¶</a> – a mapped class, or the actual <a class="reference internal" href="mapping_api.html#sqlalchemy.orm.mapper.Mapper" title="sqlalchemy.orm.mapper.Mapper"><code class="xref py py-class docutils literal notranslate"><span class="pre">Mapper</span></code></a> object,
representing the single kind of object represented within the mapping
list.</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.Session.bulk_update_mappings.params.mappings"></span><strong>mappings</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.bulk_update_mappings.params.mappings">¶</a> – a list of dictionaries, each one containing the state
of the mapped row to be updated, in terms of the attribute names
on the mapped class.   If the mapping refers to multiple tables,
such as a joined-inheritance mapping, each dictionary may contain
keys corresponding to all tables.   All those keys which are present
and are not part of the primary key are applied to the SET clause
of the UPDATE statement; the primary key values, which are required,
are applied to the WHERE clause.</p></li>
</ul>
</dd>
</dl>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="persistence_techniques.html#bulk-operations"><span class="std std-ref">Bulk Operations</span></a></p>
<p><a class="reference internal" href="#sqlalchemy.orm.session.Session.bulk_insert_mappings" title="sqlalchemy.orm.session.Session.bulk_insert_mappings"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.bulk_insert_mappings()</span></code></a></p>
<p><a class="reference internal" href="#sqlalchemy.orm.session.Session.bulk_save_objects" title="sqlalchemy.orm.session.Session.bulk_save_objects"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.bulk_save_objects()</span></code></a></p>
</div>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.orm.session.Session.close">
<code class="descname">close</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.Session.close" title="Permalink to this definition">¶</a></dt>
<dd><p>Close this Session.</p>
<p>This clears all items and ends any transaction in progress.</p>
<p>If this session were created with <code class="docutils literal notranslate"><span class="pre">autocommit=False</span></code>, a new
transaction is immediately begun.  Note that this new transaction does
not use any connection resources until they are first needed.</p>
</dd></dl>

<dl class="classmethod">
<dt id="sqlalchemy.orm.session.Session.close_all">
<em class="property">classmethod </em><code class="descname">close_all</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.Session.close_all" title="Permalink to this definition">¶</a></dt>
<dd><div class="inherited-member docutils container">
<p><em>inherited from the</em> <code class="xref py py-meth docutils literal notranslate"><span class="pre">close_all()</span></code> <em>method of</em> <code class="xref py py-class docutils literal notranslate"><span class="pre">_SessionClassMethods</span></code></p>
</div>
<p>Close <em>all</em> sessions in memory.</p>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.orm.session.Session.commit">
<code class="descname">commit</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.Session.commit" title="Permalink to this definition">¶</a></dt>
<dd><p>Flush pending changes and commit the current transaction.</p>
<p>If no transaction is in progress, this method raises an
<a class="reference internal" href="../core/exceptions.html#sqlalchemy.exc.InvalidRequestError" title="sqlalchemy.exc.InvalidRequestError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">InvalidRequestError</span></code></a>.</p>
<p>By default, the <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> also expires all database
loaded state on all ORM-managed attributes after transaction commit.
This so that subsequent operations load the most recent
data from the database.   This behavior can be disabled using
the <code class="docutils literal notranslate"><span class="pre">expire_on_commit=False</span></code> option to <a class="reference internal" href="#sqlalchemy.orm.session.sessionmaker" title="sqlalchemy.orm.session.sessionmaker"><code class="xref py py-class docutils literal notranslate"><span class="pre">sessionmaker</span></code></a> or
the <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> constructor.</p>
<p>If a subtransaction is in effect (which occurs when begin() is called
multiple times), the subtransaction will be closed, and the next call
to <code class="docutils literal notranslate"><span class="pre">commit()</span></code> will operate on the enclosing transaction.</p>
<p>When using the <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> in its default mode of
<code class="docutils literal notranslate"><span class="pre">autocommit=False</span></code>, a new transaction will
be begun immediately after the commit, but note that the newly begun
transaction does <em>not</em> use any connection resources until the first
SQL is actually emitted.</p>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="session_basics.html#session-committing"><span class="std std-ref">Committing</span></a></p>
</div>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.orm.session.Session.connection">
<code class="descname">connection</code><span class="sig-paren">(</span><em>mapper=None</em>, <em>clause=None</em>, <em>bind=None</em>, <em>close_with_result=False</em>, <em>execution_options=None</em>, <em>**kw</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.Session.connection" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Connection" title="sqlalchemy.engine.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a> object corresponding to this
<a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> object’s transactional state.</p>
<p>If this <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> is configured with <code class="docutils literal notranslate"><span class="pre">autocommit=False</span></code>,
either the <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Connection" title="sqlalchemy.engine.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a> corresponding to the current
transaction is returned, or if no transaction is in progress, a new
one is begun and the <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Connection" title="sqlalchemy.engine.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a> returned (note that no
transactional state is established with the DBAPI until the first
SQL statement is emitted).</p>
<p>Alternatively, if this <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> is configured with
<code class="docutils literal notranslate"><span class="pre">autocommit=True</span></code>, an ad-hoc <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Connection" title="sqlalchemy.engine.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a> is returned
using <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Engine.contextual_connect" title="sqlalchemy.engine.Engine.contextual_connect"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Engine.contextual_connect()</span></code></a> on the underlying
<a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Engine</span></code></a>.</p>
<p>Ambiguity in multi-bind or unbound <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> objects can be
resolved through any of the optional keyword arguments.   This
ultimately makes usage of the <a class="reference internal" href="#sqlalchemy.orm.session.Session.get_bind" title="sqlalchemy.orm.session.Session.get_bind"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_bind()</span></code></a> method for resolution.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><span class="target" id="sqlalchemy.orm.session.Session.connection.params.bind"></span><strong>bind</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.connection.params.bind">¶</a> – Optional <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Engine</span></code></a> to be used as the bind.  If
this engine is already involved in an ongoing transaction,
that connection will be used.  This argument takes precedence
over <code class="docutils literal notranslate"><span class="pre">mapper</span></code>, <code class="docutils literal notranslate"><span class="pre">clause</span></code>.</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.Session.connection.params.mapper"></span><strong>mapper</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.connection.params.mapper">¶</a> – Optional <a class="reference internal" href="mapping_api.html#sqlalchemy.orm.mapper" title="sqlalchemy.orm.mapper"><code class="xref py py-func docutils literal notranslate"><span class="pre">mapper()</span></code></a> mapped class, used to identify
the appropriate bind.  This argument takes precedence over
<code class="docutils literal notranslate"><span class="pre">clause</span></code>.</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.Session.connection.params.clause"></span><strong>clause</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.connection.params.clause">¶</a> – A <a class="reference internal" href="../core/sqlelement.html#sqlalchemy.sql.expression.ClauseElement" title="sqlalchemy.sql.expression.ClauseElement"><code class="xref py py-class docutils literal notranslate"><span class="pre">ClauseElement</span></code></a> (i.e. <a class="reference internal" href="../core/selectable.html#sqlalchemy.sql.expression.select" title="sqlalchemy.sql.expression.select"><code class="xref py py-func docutils literal notranslate"><span class="pre">select()</span></code></a>,
<a class="reference internal" href="../core/sqlelement.html#sqlalchemy.sql.expression.text" title="sqlalchemy.sql.expression.text"><code class="xref py py-func docutils literal notranslate"><span class="pre">text()</span></code></a>,
etc.) which will be used to locate a bind, if a bind
cannot otherwise be identified.</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.Session.connection.params.close_with_result"></span><strong>close_with_result</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.connection.params.close_with_result">¶</a> – Passed to <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Engine.connect" title="sqlalchemy.engine.Engine.connect"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Engine.connect()</span></code></a>,
indicating the <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Connection" title="sqlalchemy.engine.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a> should be considered
“single use”, automatically closing when the first result set is
closed.  This flag only has an effect if this <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> is
configured with <code class="docutils literal notranslate"><span class="pre">autocommit=True</span></code> and does not already have a
transaction in progress.</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.Session.connection.params.execution_options"></span><strong>execution_options</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.connection.params.execution_options">¶</a> – <p>a dictionary of execution options that will
be passed to <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Connection.execution_options" title="sqlalchemy.engine.Connection.execution_options"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Connection.execution_options()</span></code></a>, <strong>when the
connection is first procured only</strong>.   If the connection is already
present within the <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a>, a warning is emitted and
the arguments are ignored.</p>
<div class="versionadded">
<p><span class="versionmodified added">New in version 0.9.9.</span></p>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="session_transaction.html#session-transaction-isolation"><span class="std std-ref">Setting Transaction Isolation Levels</span></a></p>
</div>
</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.Session.connection.params.**kw"></span><strong>**kw</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.connection.params.**kw">¶</a> – Additional keyword arguments are sent to <a class="reference internal" href="#sqlalchemy.orm.session.Session.get_bind" title="sqlalchemy.orm.session.Session.get_bind"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_bind()</span></code></a>,
allowing additional arguments to be passed to custom
implementations of <a class="reference internal" href="#sqlalchemy.orm.session.Session.get_bind" title="sqlalchemy.orm.session.Session.get_bind"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_bind()</span></code></a>.</p></li>
</ul>
</dd>
</dl>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.orm.session.Session.delete">
<code class="descname">delete</code><span class="sig-paren">(</span><em>instance</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.Session.delete" title="Permalink to this definition">¶</a></dt>
<dd><p>Mark an instance as deleted.</p>
<p>The database delete operation occurs upon <code class="docutils literal notranslate"><span class="pre">flush()</span></code>.</p>
</dd></dl>

<dl class="attribute">
<dt id="sqlalchemy.orm.session.Session.deleted">
<code class="descname">deleted</code><a class="headerlink" href="#sqlalchemy.orm.session.Session.deleted" title="Permalink to this definition">¶</a></dt>
<dd><p>The set of all instances marked as ‘deleted’ within this <code class="docutils literal notranslate"><span class="pre">Session</span></code></p>
</dd></dl>

<dl class="attribute">
<dt id="sqlalchemy.orm.session.Session.dirty">
<code class="descname">dirty</code><a class="headerlink" href="#sqlalchemy.orm.session.Session.dirty" title="Permalink to this definition">¶</a></dt>
<dd><p>The set of all persistent instances considered dirty.</p>
<p>E.g.:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">some_mapped_object</span> <span class="ow">in</span> <span class="n">session</span><span class="o">.</span><span class="n">dirty</span></pre></div>
</div>
<p>Instances are considered dirty when they were modified but not
deleted.</p>
<p>Note that this ‘dirty’ calculation is ‘optimistic’; most
attribute-setting or collection modification operations will
mark an instance as ‘dirty’ and place it in this set, even if
there is no net change to the attribute’s value.  At flush
time, the value of each attribute is compared to its
previously saved value, and if there’s no net change, no SQL
operation will occur (this is a more expensive operation so
it’s only done at flush time).</p>
<p>To check if an instance has actionable net changes to its
attributes, use the <a class="reference internal" href="#sqlalchemy.orm.session.Session.is_modified" title="sqlalchemy.orm.session.Session.is_modified"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.is_modified()</span></code></a> method.</p>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.orm.session.Session.enable_relationship_loading">
<code class="descname">enable_relationship_loading</code><span class="sig-paren">(</span><em>obj</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.Session.enable_relationship_loading" title="Permalink to this definition">¶</a></dt>
<dd><p>Associate an object with this <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> for related
object loading.</p>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p><a class="reference internal" href="#sqlalchemy.orm.session.Session.enable_relationship_loading" title="sqlalchemy.orm.session.Session.enable_relationship_loading"><code class="xref py py-meth docutils literal notranslate"><span class="pre">enable_relationship_loading()</span></code></a> exists to serve special
use cases and is not recommended for general use.</p>
</div>
<p>Accesses of attributes mapped with <a class="reference internal" href="relationship_api.html#sqlalchemy.orm.relationship" title="sqlalchemy.orm.relationship"><code class="xref py py-func docutils literal notranslate"><span class="pre">relationship()</span></code></a>
will attempt to load a value from the database using this
<a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> as the source of connectivity.  The values
will be loaded based on foreign key and primary key values
present on this object - if not present, then those relationships
will be unavailable.</p>
<p>The object will be attached to this session, but will
<strong>not</strong> participate in any persistence operations; its state
for almost all purposes will remain either “transient” or
“detached”, except for the case of relationship loading.</p>
<p>Also note that backrefs will often not work as expected.
Altering a relationship-bound attribute on the target object
may not fire off a backref event, if the effective value
is what was already loaded from a foreign-key-holding value.</p>
<p>The <a class="reference internal" href="#sqlalchemy.orm.session.Session.enable_relationship_loading" title="sqlalchemy.orm.session.Session.enable_relationship_loading"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.enable_relationship_loading()</span></code></a> method is
similar to the <code class="docutils literal notranslate"><span class="pre">load_on_pending</span></code> flag on <a class="reference internal" href="relationship_api.html#sqlalchemy.orm.relationship" title="sqlalchemy.orm.relationship"><code class="xref py py-func docutils literal notranslate"><span class="pre">relationship()</span></code></a>.
Unlike that flag, <a class="reference internal" href="#sqlalchemy.orm.session.Session.enable_relationship_loading" title="sqlalchemy.orm.session.Session.enable_relationship_loading"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.enable_relationship_loading()</span></code></a> allows
an object to remain transient while still being able to load
related items.</p>
<p>To make a transient object associated with a <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a>
via <a class="reference internal" href="#sqlalchemy.orm.session.Session.enable_relationship_loading" title="sqlalchemy.orm.session.Session.enable_relationship_loading"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.enable_relationship_loading()</span></code></a> pending, add
it to the <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> using <a class="reference internal" href="#sqlalchemy.orm.session.Session.add" title="sqlalchemy.orm.session.Session.add"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.add()</span></code></a> normally.
If the object instead represents an existing identity in the database,
it should be merged using <a class="reference internal" href="#sqlalchemy.orm.session.Session.merge" title="sqlalchemy.orm.session.Session.merge"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.merge()</span></code></a>.</p>
<p><a class="reference internal" href="#sqlalchemy.orm.session.Session.enable_relationship_loading" title="sqlalchemy.orm.session.Session.enable_relationship_loading"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.enable_relationship_loading()</span></code></a> does not improve
behavior when the ORM is used normally - object references should be
constructed at the object level, not at the foreign key level, so
that they are present in an ordinary way before flush()
proceeds.  This method is not intended for general use.</p>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><code class="docutils literal notranslate"><span class="pre">load_on_pending</span></code> at <a class="reference internal" href="relationship_api.html#sqlalchemy.orm.relationship" title="sqlalchemy.orm.relationship"><code class="xref py py-func docutils literal notranslate"><span class="pre">relationship()</span></code></a> - this flag
allows per-relationship loading of many-to-ones on items that
are pending.</p>
<p><a class="reference internal" href="#sqlalchemy.orm.session.make_transient_to_detached" title="sqlalchemy.orm.session.make_transient_to_detached"><code class="xref py py-func docutils literal notranslate"><span class="pre">make_transient_to_detached()</span></code></a> - allows for an object to
be added to a <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> without SQL emitted, which then
will unexpire attributes on access.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.orm.session.Session.execute">
<code class="descname">execute</code><span class="sig-paren">(</span><em>clause</em>, <em>params=None</em>, <em>mapper=None</em>, <em>bind=None</em>, <em>**kw</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.Session.execute" title="Permalink to this definition">¶</a></dt>
<dd><p>Execute a SQL expression construct or string statement within
the current transaction.</p>
<p>Returns a <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.ResultProxy" title="sqlalchemy.engine.ResultProxy"><code class="xref py py-class docutils literal notranslate"><span class="pre">ResultProxy</span></code></a> representing
results of the statement execution, in the same manner as that of an
<a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Engine</span></code></a> or
<a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Connection" title="sqlalchemy.engine.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a>.</p>
<p>E.g.:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">result</span> <span class="o">=</span> <span class="n">session</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span>
            <span class="n">user_table</span><span class="o">.</span><span class="n">select</span><span class="p">()</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">user_table</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">id</span> <span class="o">==</span> <span class="mi">5</span><span class="p">)</span>
        <span class="p">)</span></pre></div>
</div>
<p><a class="reference internal" href="#sqlalchemy.orm.session.Session.execute" title="sqlalchemy.orm.session.Session.execute"><code class="xref py py-meth docutils literal notranslate"><span class="pre">execute()</span></code></a> accepts any executable clause construct,
such as <a class="reference internal" href="../core/selectable.html#sqlalchemy.sql.expression.select" title="sqlalchemy.sql.expression.select"><code class="xref py py-func docutils literal notranslate"><span class="pre">select()</span></code></a>,
<a class="reference internal" href="../core/dml.html#sqlalchemy.sql.expression.insert" title="sqlalchemy.sql.expression.insert"><code class="xref py py-func docutils literal notranslate"><span class="pre">insert()</span></code></a>,
<a class="reference internal" href="../core/dml.html#sqlalchemy.sql.expression.update" title="sqlalchemy.sql.expression.update"><code class="xref py py-func docutils literal notranslate"><span class="pre">update()</span></code></a>,
<a class="reference internal" href="../core/dml.html#sqlalchemy.sql.expression.delete" title="sqlalchemy.sql.expression.delete"><code class="xref py py-func docutils literal notranslate"><span class="pre">delete()</span></code></a>, and
<a class="reference internal" href="../core/sqlelement.html#sqlalchemy.sql.expression.text" title="sqlalchemy.sql.expression.text"><code class="xref py py-func docutils literal notranslate"><span class="pre">text()</span></code></a>.  Plain SQL strings can be passed
as well, which in the case of <a class="reference internal" href="#sqlalchemy.orm.session.Session.execute" title="sqlalchemy.orm.session.Session.execute"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.execute()</span></code></a> only
will be interpreted the same as if it were passed via a
<a class="reference internal" href="../core/sqlelement.html#sqlalchemy.sql.expression.text" title="sqlalchemy.sql.expression.text"><code class="xref py py-func docutils literal notranslate"><span class="pre">text()</span></code></a> construct.  That is, the following usage:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">result</span> <span class="o">=</span> <span class="n">session</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span>
            <span class="s2">&quot;SELECT * FROM user WHERE id=:param&quot;</span><span class="p">,</span>
            <span class="p">{</span><span class="s2">&quot;param&quot;</span><span class="p">:</span><span class="mi">5</span><span class="p">}</span>
        <span class="p">)</span></pre></div>
</div>
<p>is equivalent to:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">sqlalchemy</span> <span class="k">import</span> <span class="n">text</span>
<span class="n">result</span> <span class="o">=</span> <span class="n">session</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span>
            <span class="n">text</span><span class="p">(</span><span class="s2">&quot;SELECT * FROM user WHERE id=:param&quot;</span><span class="p">),</span>
            <span class="p">{</span><span class="s2">&quot;param&quot;</span><span class="p">:</span><span class="mi">5</span><span class="p">}</span>
        <span class="p">)</span></pre></div>
</div>
<p>The second positional argument to <a class="reference internal" href="#sqlalchemy.orm.session.Session.execute" title="sqlalchemy.orm.session.Session.execute"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.execute()</span></code></a> is an
optional parameter set.  Similar to that of
<a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Connection.execute" title="sqlalchemy.engine.Connection.execute"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Connection.execute()</span></code></a>, whether this is passed as a single
dictionary, or a list of dictionaries, determines whether the DBAPI
cursor’s <code class="docutils literal notranslate"><span class="pre">execute()</span></code> or <code class="docutils literal notranslate"><span class="pre">executemany()</span></code> is used to execute the
statement.   An INSERT construct may be invoked for a single row:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">result</span> <span class="o">=</span> <span class="n">session</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span>
    <span class="n">users</span><span class="o">.</span><span class="n">insert</span><span class="p">(),</span> <span class="p">{</span><span class="s2">&quot;id&quot;</span><span class="p">:</span> <span class="mi">7</span><span class="p">,</span> <span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;somename&quot;</span><span class="p">})</span></pre></div>
</div>
<p>or for multiple rows:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">result</span> <span class="o">=</span> <span class="n">session</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">users</span><span class="o">.</span><span class="n">insert</span><span class="p">(),</span> <span class="p">[</span>
                        <span class="p">{</span><span class="s2">&quot;id&quot;</span><span class="p">:</span> <span class="mi">7</span><span class="p">,</span> <span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;somename7&quot;</span><span class="p">},</span>
                        <span class="p">{</span><span class="s2">&quot;id&quot;</span><span class="p">:</span> <span class="mi">8</span><span class="p">,</span> <span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;somename8&quot;</span><span class="p">},</span>
                        <span class="p">{</span><span class="s2">&quot;id&quot;</span><span class="p">:</span> <span class="mi">9</span><span class="p">,</span> <span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;somename9&quot;</span><span class="p">}</span>
                    <span class="p">])</span></pre></div>
</div>
<p>The statement is executed within the current transactional context of
this <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a>.   The <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Connection" title="sqlalchemy.engine.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a> which is used
to execute the statement can also be acquired directly by
calling the <a class="reference internal" href="#sqlalchemy.orm.session.Session.connection" title="sqlalchemy.orm.session.Session.connection"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.connection()</span></code></a> method.  Both methods use
a rule-based resolution scheme in order to determine the
<a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Connection" title="sqlalchemy.engine.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a>, which in the average case is derived directly
from the “bind” of the <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> itself, and in other cases
can be based on the <a class="reference internal" href="mapping_api.html#sqlalchemy.orm.mapper" title="sqlalchemy.orm.mapper"><code class="xref py py-func docutils literal notranslate"><span class="pre">mapper()</span></code></a>
and <a class="reference internal" href="../core/metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><code class="xref py py-class docutils literal notranslate"><span class="pre">Table</span></code></a> objects passed to the method; see the
documentation for <a class="reference internal" href="#sqlalchemy.orm.session.Session.get_bind" title="sqlalchemy.orm.session.Session.get_bind"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.get_bind()</span></code></a> for a full description of
this scheme.</p>
<p>The <a class="reference internal" href="#sqlalchemy.orm.session.Session.execute" title="sqlalchemy.orm.session.Session.execute"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.execute()</span></code></a> method does <em>not</em> invoke autoflush.</p>
<p>The <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.ResultProxy" title="sqlalchemy.engine.ResultProxy"><code class="xref py py-class docutils literal notranslate"><span class="pre">ResultProxy</span></code></a> returned by the <a class="reference internal" href="#sqlalchemy.orm.session.Session.execute" title="sqlalchemy.orm.session.Session.execute"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.execute()</span></code></a>
method is returned with the “close_with_result” flag set to true;
the significance of this flag is that if this <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> is
autocommitting and does not have a transaction-dedicated
<a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Connection" title="sqlalchemy.engine.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a> available, a temporary <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Connection" title="sqlalchemy.engine.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a> is
established for the statement execution, which is closed (meaning,
returned to the connection pool) when the <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.ResultProxy" title="sqlalchemy.engine.ResultProxy"><code class="xref py py-class docutils literal notranslate"><span class="pre">ResultProxy</span></code></a> has
consumed all available data. This applies <em>only</em> when the
<a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> is configured with autocommit=True and no
transaction has been started.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><span class="target" id="sqlalchemy.orm.session.Session.execute.params.clause"></span><strong>clause</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.execute.params.clause">¶</a> – An executable statement (i.e. an <a class="reference internal" href="../core/selectable.html#sqlalchemy.sql.expression.Executable" title="sqlalchemy.sql.expression.Executable"><code class="xref py py-class docutils literal notranslate"><span class="pre">Executable</span></code></a> expression
such as <a class="reference internal" href="../core/selectable.html#sqlalchemy.sql.expression.select" title="sqlalchemy.sql.expression.select"><code class="xref py py-func docutils literal notranslate"><span class="pre">expression.select()</span></code></a>) or string SQL statement
to be executed.</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.Session.execute.params.params"></span><strong>params</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.execute.params.params">¶</a> – Optional dictionary, or list of dictionaries, containing
bound parameter values.   If a single dictionary, single-row
execution occurs; if a list of dictionaries, an
“executemany” will be invoked.  The keys in each dictionary
must correspond to parameter names present in the statement.</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.Session.execute.params.mapper"></span><strong>mapper</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.execute.params.mapper">¶</a> – Optional <a class="reference internal" href="mapping_api.html#sqlalchemy.orm.mapper" title="sqlalchemy.orm.mapper"><code class="xref py py-func docutils literal notranslate"><span class="pre">mapper()</span></code></a> or mapped class, used to identify
the appropriate bind.  This argument takes precedence over
<code class="docutils literal notranslate"><span class="pre">clause</span></code> when locating a bind.   See <a class="reference internal" href="#sqlalchemy.orm.session.Session.get_bind" title="sqlalchemy.orm.session.Session.get_bind"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.get_bind()</span></code></a>
for more details.</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.Session.execute.params.bind"></span><strong>bind</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.execute.params.bind">¶</a> – Optional <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Engine</span></code></a> to be used as the bind.  If
this engine is already involved in an ongoing transaction,
that connection will be used.  This argument takes
precedence over <code class="docutils literal notranslate"><span class="pre">mapper</span></code> and <code class="docutils literal notranslate"><span class="pre">clause</span></code> when locating
a bind.</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.Session.execute.params.**kw"></span><strong>**kw</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.execute.params.**kw">¶</a> – Additional keyword arguments are sent to <a class="reference internal" href="#sqlalchemy.orm.session.Session.get_bind" title="sqlalchemy.orm.session.Session.get_bind"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.get_bind()</span></code></a>
to allow extensibility of “bind” schemes.</p></li>
</ul>
</dd>
</dl>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="../core/tutorial.html"><span class="std std-ref">SQL Expression Language Tutorial</span></a> - Tutorial on using Core SQL
constructs.</p>
<p><a class="reference internal" href="../core/connections.html"><span class="std std-ref">Working with Engines and Connections</span></a> - Further information on direct
statement execution.</p>
<p><a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Connection.execute" title="sqlalchemy.engine.Connection.execute"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Connection.execute()</span></code></a> - core level statement execution
method, which is <a class="reference internal" href="#sqlalchemy.orm.session.Session.execute" title="sqlalchemy.orm.session.Session.execute"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.execute()</span></code></a> ultimately uses
in order to execute the statement.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.orm.session.Session.expire">
<code class="descname">expire</code><span class="sig-paren">(</span><em>instance</em>, <em>attribute_names=None</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.Session.expire" title="Permalink to this definition">¶</a></dt>
<dd><p>Expire the attributes on an instance.</p>
<p>Marks the attributes of an instance as out of date. When an expired
attribute is next accessed, a query will be issued to the
<a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> object’s current transactional context in order to
load all expired attributes for the given instance.   Note that
a highly isolated transaction will return the same values as were
previously read in that same transaction, regardless of changes
in database state outside of that transaction.</p>
<p>To expire all objects in the <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> simultaneously,
use <a class="reference internal" href="#sqlalchemy.orm.session.Session.expire_all" title="sqlalchemy.orm.session.Session.expire_all"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.expire_all()</span></code></a>.</p>
<p>The <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> object’s default behavior is to
expire all state whenever the <a class="reference internal" href="#sqlalchemy.orm.session.Session.rollback" title="sqlalchemy.orm.session.Session.rollback"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.rollback()</span></code></a>
or <a class="reference internal" href="#sqlalchemy.orm.session.Session.commit" title="sqlalchemy.orm.session.Session.commit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.commit()</span></code></a> methods are called, so that new
state can be loaded for the new transaction.   For this reason,
calling <a class="reference internal" href="#sqlalchemy.orm.session.Session.expire" title="sqlalchemy.orm.session.Session.expire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.expire()</span></code></a> only makes sense for the specific
case that a non-ORM SQL statement was emitted in the current
transaction.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><span class="target" id="sqlalchemy.orm.session.Session.expire.params.instance"></span><strong>instance</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.expire.params.instance">¶</a> – The instance to be refreshed.</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.Session.expire.params.attribute_names"></span><strong>attribute_names</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.expire.params.attribute_names">¶</a> – optional list of string attribute names
indicating a subset of attributes to be expired.</p></li>
</ul>
</dd>
</dl>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="session_state_management.html#session-expire"><span class="std std-ref">Refreshing / Expiring</span></a> - introductory material</p>
<p><a class="reference internal" href="#sqlalchemy.orm.session.Session.expire" title="sqlalchemy.orm.session.Session.expire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.expire()</span></code></a></p>
<p><a class="reference internal" href="#sqlalchemy.orm.session.Session.refresh" title="sqlalchemy.orm.session.Session.refresh"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.refresh()</span></code></a></p>
</div>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.orm.session.Session.expire_all">
<code class="descname">expire_all</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.Session.expire_all" title="Permalink to this definition">¶</a></dt>
<dd><p>Expires all persistent instances within this Session.</p>
<p>When any attributes on a persistent instance is next accessed,
a query will be issued using the
<a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> object’s current transactional context in order to
load all expired attributes for the given instance.   Note that
a highly isolated transaction will return the same values as were
previously read in that same transaction, regardless of changes
in database state outside of that transaction.</p>
<p>To expire individual objects and individual attributes
on those objects, use <a class="reference internal" href="#sqlalchemy.orm.session.Session.expire" title="sqlalchemy.orm.session.Session.expire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.expire()</span></code></a>.</p>
<p>The <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> object’s default behavior is to
expire all state whenever the <a class="reference internal" href="#sqlalchemy.orm.session.Session.rollback" title="sqlalchemy.orm.session.Session.rollback"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.rollback()</span></code></a>
or <a class="reference internal" href="#sqlalchemy.orm.session.Session.commit" title="sqlalchemy.orm.session.Session.commit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.commit()</span></code></a> methods are called, so that new
state can be loaded for the new transaction.   For this reason,
calling <a class="reference internal" href="#sqlalchemy.orm.session.Session.expire_all" title="sqlalchemy.orm.session.Session.expire_all"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.expire_all()</span></code></a> should not be needed when
autocommit is <code class="docutils literal notranslate"><span class="pre">False</span></code>, assuming the transaction is isolated.</p>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="session_state_management.html#session-expire"><span class="std std-ref">Refreshing / Expiring</span></a> - introductory material</p>
<p><a class="reference internal" href="#sqlalchemy.orm.session.Session.expire" title="sqlalchemy.orm.session.Session.expire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.expire()</span></code></a></p>
<p><a class="reference internal" href="#sqlalchemy.orm.session.Session.refresh" title="sqlalchemy.orm.session.Session.refresh"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.refresh()</span></code></a></p>
</div>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.orm.session.Session.expunge">
<code class="descname">expunge</code><span class="sig-paren">(</span><em>instance</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.Session.expunge" title="Permalink to this definition">¶</a></dt>
<dd><p>Remove the <cite>instance</cite> from this <code class="docutils literal notranslate"><span class="pre">Session</span></code>.</p>
<p>This will free all internal references to the instance.  Cascading
will be applied according to the <em>expunge</em> cascade rule.</p>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.orm.session.Session.expunge_all">
<code class="descname">expunge_all</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.Session.expunge_all" title="Permalink to this definition">¶</a></dt>
<dd><p>Remove all object instances from this <code class="docutils literal notranslate"><span class="pre">Session</span></code>.</p>
<p>This is equivalent to calling <code class="docutils literal notranslate"><span class="pre">expunge(obj)</span></code> on all objects in this
<code class="docutils literal notranslate"><span class="pre">Session</span></code>.</p>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.orm.session.Session.flush">
<code class="descname">flush</code><span class="sig-paren">(</span><em>objects=None</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.Session.flush" title="Permalink to this definition">¶</a></dt>
<dd><p>Flush all the object changes to the database.</p>
<p>Writes out all pending object creations, deletions and modifications
to the database as INSERTs, DELETEs, UPDATEs, etc.  Operations are
automatically ordered by the Session’s unit of work dependency
solver.</p>
<p>Database operations will be issued in the current transactional
context and do not affect the state of the transaction, unless an
error occurs, in which case the entire transaction is rolled back.
You may flush() as often as you like within a transaction to move
changes from Python to the database’s transaction buffer.</p>
<p>For <code class="docutils literal notranslate"><span class="pre">autocommit</span></code> Sessions with no active manual transaction, flush()
will create a transaction on the fly that surrounds the entire set of
operations into the flush.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><span class="target" id="sqlalchemy.orm.session.Session.flush.params.objects"></span><strong>objects</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.flush.params.objects">¶</a> – <p>Optional; restricts the flush operation to operate
only on elements that are in the given collection.</p>
<p>This feature is for an extremely narrow set of use cases where
particular objects may need to be operated upon before the
full flush() occurs.  It is not intended for general use.</p>
</p>
</dd>
</dl>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.orm.session.Session.get_bind">
<code class="descname">get_bind</code><span class="sig-paren">(</span><em>mapper=None</em>, <em>clause=None</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.Session.get_bind" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a “bind” to which this <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> is bound.</p>
<p>The “bind” is usually an instance of <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Engine</span></code></a>,
except in the case where the <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> has been
explicitly bound directly to a <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Connection" title="sqlalchemy.engine.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a>.</p>
<p>For a multiply-bound or unbound <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a>, the
<code class="docutils literal notranslate"><span class="pre">mapper</span></code> or <code class="docutils literal notranslate"><span class="pre">clause</span></code> arguments are used to determine the
appropriate bind to return.</p>
<p>Note that the “mapper” argument is usually present
when <a class="reference internal" href="#sqlalchemy.orm.session.Session.get_bind" title="sqlalchemy.orm.session.Session.get_bind"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.get_bind()</span></code></a> is called via an ORM
operation such as a <a class="reference internal" href="#sqlalchemy.orm.session.Session.query" title="sqlalchemy.orm.session.Session.query"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.query()</span></code></a>, each
individual INSERT/UPDATE/DELETE operation within a
<a class="reference internal" href="#sqlalchemy.orm.session.Session.flush" title="sqlalchemy.orm.session.Session.flush"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.flush()</span></code></a>, call, etc.</p>
<p>The order of resolution is:</p>
<ol class="arabic simple">
<li><p>if mapper given and session.binds is present,
locate a bind based first on the mapper in use, then
on the mapped class in use, then on any base classes that are
present in the <code class="docutils literal notranslate"><span class="pre">__mro__</span></code> of the mapped class, from more specific
superclasses to more general.</p></li>
<li><p>if clause given and session.binds is present,
locate a bind based on <a class="reference internal" href="../core/metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><code class="xref py py-class docutils literal notranslate"><span class="pre">Table</span></code></a> objects
found in the given clause present in session.binds.</p></li>
<li><p>if session.bind is present, return that.</p></li>
<li><p>if clause given, attempt to return a bind
linked to the <a class="reference internal" href="../core/metadata.html#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><code class="xref py py-class docutils literal notranslate"><span class="pre">MetaData</span></code></a> ultimately
associated with the clause.</p></li>
<li><p>if mapper given, attempt to return a bind
linked to the <a class="reference internal" href="../core/metadata.html#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><code class="xref py py-class docutils literal notranslate"><span class="pre">MetaData</span></code></a> ultimately
associated with the <a class="reference internal" href="../core/metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><code class="xref py py-class docutils literal notranslate"><span class="pre">Table</span></code></a> or other
selectable to which the mapper is mapped.</p></li>
<li><p>No bind can be found, <a class="reference internal" href="../core/exceptions.html#sqlalchemy.exc.UnboundExecutionError" title="sqlalchemy.exc.UnboundExecutionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnboundExecutionError</span></code></a>
is raised.</p></li>
</ol>
<p>Note that the <a class="reference internal" href="#sqlalchemy.orm.session.Session.get_bind" title="sqlalchemy.orm.session.Session.get_bind"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.get_bind()</span></code></a> method can be overridden on
a user-defined subclass of <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> to provide any kind
of bind resolution scheme.  See the example at
<a class="reference internal" href="persistence_techniques.html#session-custom-partitioning"><span class="std std-ref">Custom Vertical Partitioning</span></a>.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><span class="target" id="sqlalchemy.orm.session.Session.get_bind.params.mapper"></span><strong>mapper</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.get_bind.params.mapper">¶</a> – Optional <a class="reference internal" href="mapping_api.html#sqlalchemy.orm.mapper" title="sqlalchemy.orm.mapper"><code class="xref py py-func docutils literal notranslate"><span class="pre">mapper()</span></code></a> mapped class or instance of
<a class="reference internal" href="mapping_api.html#sqlalchemy.orm.mapper.Mapper" title="sqlalchemy.orm.mapper.Mapper"><code class="xref py py-class docutils literal notranslate"><span class="pre">Mapper</span></code></a>.   The bind can be derived from a <a class="reference internal" href="mapping_api.html#sqlalchemy.orm.mapper.Mapper" title="sqlalchemy.orm.mapper.Mapper"><code class="xref py py-class docutils literal notranslate"><span class="pre">Mapper</span></code></a>
first by consulting the “binds” map associated with this
<a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a>, and secondly by consulting the <a class="reference internal" href="../core/metadata.html#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><code class="xref py py-class docutils literal notranslate"><span class="pre">MetaData</span></code></a>
associated with the <a class="reference internal" href="../core/metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><code class="xref py py-class docutils literal notranslate"><span class="pre">Table</span></code></a> to which the <a class="reference internal" href="mapping_api.html#sqlalchemy.orm.mapper.Mapper" title="sqlalchemy.orm.mapper.Mapper"><code class="xref py py-class docutils literal notranslate"><span class="pre">Mapper</span></code></a>
is mapped for a bind.</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.Session.get_bind.params.clause"></span><strong>clause</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.get_bind.params.clause">¶</a> – A <a class="reference internal" href="../core/sqlelement.html#sqlalchemy.sql.expression.ClauseElement" title="sqlalchemy.sql.expression.ClauseElement"><code class="xref py py-class docutils literal notranslate"><span class="pre">ClauseElement</span></code></a> (i.e. <a class="reference internal" href="../core/selectable.html#sqlalchemy.sql.expression.select" title="sqlalchemy.sql.expression.select"><code class="xref py py-func docutils literal notranslate"><span class="pre">select()</span></code></a>,
<a class="reference internal" href="../core/sqlelement.html#sqlalchemy.sql.expression.text" title="sqlalchemy.sql.expression.text"><code class="xref py py-func docutils literal notranslate"><span class="pre">text()</span></code></a>,
etc.).  If the <code class="docutils literal notranslate"><span class="pre">mapper</span></code> argument is not present or could not
produce a bind, the given expression construct will be searched
for a bound element, typically a <a class="reference internal" href="../core/metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><code class="xref py py-class docutils literal notranslate"><span class="pre">Table</span></code></a> associated with
bound <a class="reference internal" href="../core/metadata.html#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><code class="xref py py-class docutils literal notranslate"><span class="pre">MetaData</span></code></a>.</p></li>
</ul>
</dd>
</dl>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="persistence_techniques.html#session-partitioning"><span class="std std-ref">Partitioning Strategies (e.g. multiple database backends per Session)</span></a></p>
<p><a class="reference internal" href="#sqlalchemy.orm.session.Session.params.binds" title="sqlalchemy.orm.session.Session"><code class="xref py py-paramref docutils literal notranslate"><span class="pre">Session.binds</span></code></a></p>
<p><a class="reference internal" href="#sqlalchemy.orm.session.Session.bind_mapper" title="sqlalchemy.orm.session.Session.bind_mapper"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.bind_mapper()</span></code></a></p>
<p><a class="reference internal" href="#sqlalchemy.orm.session.Session.bind_table" title="sqlalchemy.orm.session.Session.bind_table"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.bind_table()</span></code></a></p>
</div>
</dd></dl>

<dl class="classmethod">
<dt id="sqlalchemy.orm.session.Session.identity_key">
<em class="property">classmethod </em><code class="descname">identity_key</code><span class="sig-paren">(</span><em>orm_util</em>, <em>*args</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.Session.identity_key" title="Permalink to this definition">¶</a></dt>
<dd><div class="inherited-member docutils container">
<p><em>inherited from the</em> <code class="xref py py-meth docutils literal notranslate"><span class="pre">identity_key()</span></code> <em>method of</em> <code class="xref py py-class docutils literal notranslate"><span class="pre">_SessionClassMethods</span></code></p>
</div>
<p>Return an identity key.</p>
<p>This is an alias of <a class="reference internal" href="mapping_api.html#sqlalchemy.orm.util.identity_key" title="sqlalchemy.orm.util.identity_key"><code class="xref py py-func docutils literal notranslate"><span class="pre">util.identity_key()</span></code></a>.</p>
</dd></dl>

<dl class="attribute">
<dt id="sqlalchemy.orm.session.Session.identity_map">
<code class="descname">identity_map</code><em class="property"> = None</em><a class="headerlink" href="#sqlalchemy.orm.session.Session.identity_map" title="Permalink to this definition">¶</a></dt>
<dd><p>A mapping of object identities to objects themselves.</p>
<p>Iterating through <code class="docutils literal notranslate"><span class="pre">Session.identity_map.values()</span></code> provides
access to the full set of persistent objects (i.e., those
that have row identity) currently in the session.</p>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="mapping_api.html#sqlalchemy.orm.util.identity_key" title="sqlalchemy.orm.util.identity_key"><code class="xref py py-func docutils literal notranslate"><span class="pre">identity_key()</span></code></a> - helper function to produce the keys used
in this dictionary.</p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="sqlalchemy.orm.session.Session.info">
<code class="descname">info</code><a class="headerlink" href="#sqlalchemy.orm.session.Session.info" title="Permalink to this definition">¶</a></dt>
<dd><p>A user-modifiable dictionary.</p>
<p>The initial value of this dictionary can be populated using the
<code class="docutils literal notranslate"><span class="pre">info</span></code> argument to the <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> constructor or
<a class="reference internal" href="#sqlalchemy.orm.session.sessionmaker" title="sqlalchemy.orm.session.sessionmaker"><code class="xref py py-class docutils literal notranslate"><span class="pre">sessionmaker</span></code></a> constructor or factory methods.  The dictionary
here is always local to this <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> and can be modified
independently of all other <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> objects.</p>
<div class="versionadded">
<p><span class="versionmodified added">New in version 0.9.0.</span></p>
</div>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.orm.session.Session.invalidate">
<code class="descname">invalidate</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.Session.invalidate" title="Permalink to this definition">¶</a></dt>
<dd><p>Close this Session, using connection invalidation.</p>
<p>This is a variant of <a class="reference internal" href="#sqlalchemy.orm.session.Session.close" title="sqlalchemy.orm.session.Session.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.close()</span></code></a> that will additionally
ensure that the <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Connection.invalidate" title="sqlalchemy.engine.Connection.invalidate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Connection.invalidate()</span></code></a> method will be called
on all <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Connection" title="sqlalchemy.engine.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a> objects.  This can be called when
the database is known to be in a state where the connections are
no longer safe to be used.</p>
<p>E.g.:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">try</span><span class="p">:</span>
    <span class="n">sess</span> <span class="o">=</span> <span class="n">Session</span><span class="p">()</span>
    <span class="n">sess</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">User</span><span class="p">())</span>
    <span class="n">sess</span><span class="o">.</span><span class="n">commit</span><span class="p">()</span>
<span class="k">except</span> <span class="n">gevent</span><span class="o">.</span><span class="n">Timeout</span><span class="p">:</span>
    <span class="n">sess</span><span class="o">.</span><span class="n">invalidate</span><span class="p">()</span>
    <span class="k">raise</span>
<span class="k">except</span><span class="p">:</span>
    <span class="n">sess</span><span class="o">.</span><span class="n">rollback</span><span class="p">()</span>
    <span class="k">raise</span></pre></div>
</div>
<p>This clears all items and ends any transaction in progress.</p>
<p>If this session were created with <code class="docutils literal notranslate"><span class="pre">autocommit=False</span></code>, a new
transaction is immediately begun.  Note that this new transaction does
not use any connection resources until they are first needed.</p>
<div class="versionadded">
<p><span class="versionmodified added">New in version 0.9.9.</span></p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="sqlalchemy.orm.session.Session.is_active">
<code class="descname">is_active</code><a class="headerlink" href="#sqlalchemy.orm.session.Session.is_active" title="Permalink to this definition">¶</a></dt>
<dd><p>True if this <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> is in “transaction mode” and
is not in “partial rollback” state.</p>
<p>The <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> in its default mode of <code class="docutils literal notranslate"><span class="pre">autocommit=False</span></code>
is essentially always in “transaction mode”, in that a
<a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction" title="sqlalchemy.orm.session.SessionTransaction"><code class="xref py py-class docutils literal notranslate"><span class="pre">SessionTransaction</span></code></a> is associated with it as soon as
it is instantiated.  This <a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction" title="sqlalchemy.orm.session.SessionTransaction"><code class="xref py py-class docutils literal notranslate"><span class="pre">SessionTransaction</span></code></a> is immediately
replaced with a new one as soon as it is ended, due to a rollback,
commit, or close operation.</p>
<p>“Transaction mode” does <em>not</em> indicate whether
or not actual database connection resources are in use;  the
<a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction" title="sqlalchemy.orm.session.SessionTransaction"><code class="xref py py-class docutils literal notranslate"><span class="pre">SessionTransaction</span></code></a> object coordinates among zero or more
actual database transactions, and starts out with none, accumulating
individual DBAPI connections as different data sources are used
within its scope.   The best way to track when a particular
<a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> has actually begun to use DBAPI resources is to
implement a listener using the <a class="reference internal" href="events.html#sqlalchemy.orm.events.SessionEvents.after_begin" title="sqlalchemy.orm.events.SessionEvents.after_begin"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SessionEvents.after_begin()</span></code></a>
method, which will deliver both the <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> as well as the
target <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Connection" title="sqlalchemy.engine.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a> to a user-defined event listener.</p>
<p>The “partial rollback” state refers to when an “inner” transaction,
typically used during a flush, encounters an error and emits a
rollback of the DBAPI connection.  At this point, the
<a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> is in “partial rollback” and awaits for the user to
call <a class="reference internal" href="#sqlalchemy.orm.session.Session.rollback" title="sqlalchemy.orm.session.Session.rollback"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.rollback()</span></code></a>, in order to close out the
transaction stack.  It is in this “partial rollback” period that the
<a class="reference internal" href="#sqlalchemy.orm.session.Session.is_active" title="sqlalchemy.orm.session.Session.is_active"><code class="xref py py-attr docutils literal notranslate"><span class="pre">is_active</span></code></a> flag returns False.  After the call to
<a class="reference internal" href="#sqlalchemy.orm.session.Session.rollback" title="sqlalchemy.orm.session.Session.rollback"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.rollback()</span></code></a>, the <a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction" title="sqlalchemy.orm.session.SessionTransaction"><code class="xref py py-class docutils literal notranslate"><span class="pre">SessionTransaction</span></code></a> is
replaced with a new one and <a class="reference internal" href="#sqlalchemy.orm.session.Session.is_active" title="sqlalchemy.orm.session.Session.is_active"><code class="xref py py-attr docutils literal notranslate"><span class="pre">is_active</span></code></a> returns <code class="docutils literal notranslate"><span class="pre">True</span></code> again.</p>
<p>When a <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> is used in <code class="docutils literal notranslate"><span class="pre">autocommit=True</span></code> mode, the
<a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction" title="sqlalchemy.orm.session.SessionTransaction"><code class="xref py py-class docutils literal notranslate"><span class="pre">SessionTransaction</span></code></a> is only instantiated within the scope
of a flush call, or when <a class="reference internal" href="#sqlalchemy.orm.session.Session.begin" title="sqlalchemy.orm.session.Session.begin"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.begin()</span></code></a> is called.  So
<a class="reference internal" href="#sqlalchemy.orm.session.Session.is_active" title="sqlalchemy.orm.session.Session.is_active"><code class="xref py py-attr docutils literal notranslate"><span class="pre">is_active</span></code></a> will always be <code class="docutils literal notranslate"><span class="pre">False</span></code> outside of a flush or
<a class="reference internal" href="#sqlalchemy.orm.session.Session.begin" title="sqlalchemy.orm.session.Session.begin"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.begin()</span></code></a> block in this mode, and will be <code class="docutils literal notranslate"><span class="pre">True</span></code>
within the <a class="reference internal" href="#sqlalchemy.orm.session.Session.begin" title="sqlalchemy.orm.session.Session.begin"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.begin()</span></code></a> block as long as it doesn’t enter
“partial rollback” state.</p>
<p>From all the above, it follows that the only purpose to this flag is
for application frameworks that wish to detect is a “rollback” is
necessary within a generic error handling routine, for
<a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> objects that would otherwise be in
“partial rollback” mode.  In a typical integration case, this is also
not necessary as it is standard practice to emit
<a class="reference internal" href="#sqlalchemy.orm.session.Session.rollback" title="sqlalchemy.orm.session.Session.rollback"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.rollback()</span></code></a> unconditionally within the outermost
exception catch.</p>
<p>To track the transactional state of a <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> fully,
use event listeners, primarily the <a class="reference internal" href="events.html#sqlalchemy.orm.events.SessionEvents.after_begin" title="sqlalchemy.orm.events.SessionEvents.after_begin"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SessionEvents.after_begin()</span></code></a>,
<a class="reference internal" href="events.html#sqlalchemy.orm.events.SessionEvents.after_commit" title="sqlalchemy.orm.events.SessionEvents.after_commit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SessionEvents.after_commit()</span></code></a>,
<a class="reference internal" href="events.html#sqlalchemy.orm.events.SessionEvents.after_rollback" title="sqlalchemy.orm.events.SessionEvents.after_rollback"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SessionEvents.after_rollback()</span></code></a> and related events.</p>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.orm.session.Session.is_modified">
<code class="descname">is_modified</code><span class="sig-paren">(</span><em>instance</em>, <em>include_collections=True</em>, <em>passive=True</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.Session.is_modified" title="Permalink to this definition">¶</a></dt>
<dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the given instance has locally
modified attributes.</p>
<p>This method retrieves the history for each instrumented
attribute on the instance and performs a comparison of the current
value to its previously committed value, if any.</p>
<p>It is in effect a more expensive and accurate
version of checking for the given instance in the
<a class="reference internal" href="#sqlalchemy.orm.session.Session.dirty" title="sqlalchemy.orm.session.Session.dirty"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Session.dirty</span></code></a> collection; a full test for
each attribute’s net “dirty” status is performed.</p>
<p>E.g.:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">return</span> <span class="n">session</span><span class="o">.</span><span class="n">is_modified</span><span class="p">(</span><span class="n">someobject</span><span class="p">)</span></pre></div>
</div>
<p>A few caveats to this method apply:</p>
<ul>
<li><p>Instances present in the <a class="reference internal" href="#sqlalchemy.orm.session.Session.dirty" title="sqlalchemy.orm.session.Session.dirty"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Session.dirty</span></code></a> collection may
report <code class="docutils literal notranslate"><span class="pre">False</span></code> when tested with this method.  This is because
the object may have received change events via attribute mutation,
thus placing it in <a class="reference internal" href="#sqlalchemy.orm.session.Session.dirty" title="sqlalchemy.orm.session.Session.dirty"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Session.dirty</span></code></a>, but ultimately the state
is the same as that loaded from the database, resulting in no net
change here.</p></li>
<li><p>Scalar attributes may not have recorded the previously set
value when a new value was applied, if the attribute was not loaded,
or was expired, at the time the new value was received - in these
cases, the attribute is assumed to have a change, even if there is
ultimately no net change against its database value. SQLAlchemy in
most cases does not need the “old” value when a set event occurs, so
it skips the expense of a SQL call if the old value isn’t present,
based on the assumption that an UPDATE of the scalar value is
usually needed, and in those few cases where it isn’t, is less
expensive on average than issuing a defensive SELECT.</p>
<p>The “old” value is fetched unconditionally upon set only if the
attribute container has the <code class="docutils literal notranslate"><span class="pre">active_history</span></code> flag set to <code class="docutils literal notranslate"><span class="pre">True</span></code>.
This flag is set typically for primary key attributes and scalar
object references that are not a simple many-to-one.  To set this
flag for any arbitrary mapped column, use the <code class="docutils literal notranslate"><span class="pre">active_history</span></code>
argument with <a class="reference internal" href="mapping_columns.html#sqlalchemy.orm.column_property" title="sqlalchemy.orm.column_property"><code class="xref py py-func docutils literal notranslate"><span class="pre">column_property()</span></code></a>.</p>
</li>
</ul>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><span class="target" id="sqlalchemy.orm.session.Session.is_modified.params.instance"></span><strong>instance</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.is_modified.params.instance">¶</a> – mapped instance to be tested for pending changes.</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.Session.is_modified.params.include_collections"></span><strong>include_collections</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.is_modified.params.include_collections">¶</a> – Indicates if multivalued collections
should be included in the operation.  Setting this to <code class="docutils literal notranslate"><span class="pre">False</span></code> is a
way to detect only local-column based properties (i.e. scalar columns
or many-to-one foreign keys) that would result in an UPDATE for this
instance upon flush.</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.Session.is_modified.params.passive"></span><strong>passive</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.is_modified.params.passive">¶</a> – <div class="deprecated">
<p><span class="versionmodified deprecated">Deprecated since version 0.8: </span>The <code class="docutils literal notranslate"><span class="pre">passive</span></code> flag is deprecated and will be removed
in a future release.  The flag is no longer used and is ignored.</p>
</div>
</p></li>
</ul>
</dd>
</dl>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.orm.session.Session.merge">
<code class="descname">merge</code><span class="sig-paren">(</span><em>instance</em>, <em>load=True</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.Session.merge" title="Permalink to this definition">¶</a></dt>
<dd><p>Copy the state of a given instance into a corresponding instance
within this <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a>.</p>
<p><a class="reference internal" href="#sqlalchemy.orm.session.Session.merge" title="sqlalchemy.orm.session.Session.merge"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.merge()</span></code></a> examines the primary key attributes of the
source instance, and attempts to reconcile it with an instance of the
same primary key in the session.   If not found locally, it attempts
to load the object from the database based on primary key, and if
none can be located, creates a new instance.  The state of each
attribute on the source instance is then copied to the target
instance.  The resulting target instance is then returned by the
method; the original source instance is left unmodified, and
un-associated with the <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> if not already.</p>
<p>This operation cascades to associated instances if the association is
mapped with <code class="docutils literal notranslate"><span class="pre">cascade=&quot;merge&quot;</span></code>.</p>
<p>See <a class="reference internal" href="session_state_management.html#unitofwork-merging"><span class="std std-ref">Merging</span></a> for a detailed discussion of merging.</p>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 1.1: </span>- <a class="reference internal" href="#sqlalchemy.orm.session.Session.merge" title="sqlalchemy.orm.session.Session.merge"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.merge()</span></code></a> will now reconcile
pending objects with overlapping primary keys in the same way
as persistent.  See <a class="reference internal" href="../changelog/migration_11.html#change-3601"><span class="std std-ref">Session.merge resolves pending conflicts the same as persistent</span></a> for discussion.</p>
</div>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><span class="target" id="sqlalchemy.orm.session.Session.merge.params.instance"></span><strong>instance</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.merge.params.instance">¶</a> – Instance to be merged.</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.Session.merge.params.load"></span><strong>load</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.merge.params.load">¶</a> – <p>Boolean, when False, <a class="reference internal" href="#sqlalchemy.orm.session.Session.merge" title="sqlalchemy.orm.session.Session.merge"><code class="xref py py-meth docutils literal notranslate"><span class="pre">merge()</span></code></a> switches into
a “high performance” mode which causes it to forego emitting history
events as well as all database access.  This flag is used for
cases such as transferring graphs of objects into a <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a>
from a second level cache, or to transfer just-loaded objects
into the <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> owned by a worker thread or process
without re-querying the database.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">load=False</span></code> use case adds the caveat that the given
object has to be in a “clean” state, that is, has no pending changes
to be flushed - even if the incoming object is detached from any
<a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a>.   This is so that when
the merge operation populates local attributes and
cascades to related objects and
collections, the values can be “stamped” onto the
target object as is, without generating any history or attribute
events, and without the need to reconcile the incoming data with
any existing related objects or collections that might not
be loaded.  The resulting objects from <code class="docutils literal notranslate"><span class="pre">load=False</span></code> are always
produced as “clean”, so it is only appropriate that the given objects
should be “clean” as well, else this suggests a mis-use of the
method.</p>
</p></li>
</ul>
</dd>
</dl>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sqlalchemy.orm.session.make_transient_to_detached" title="sqlalchemy.orm.session.make_transient_to_detached"><code class="xref py py-func docutils literal notranslate"><span class="pre">make_transient_to_detached()</span></code></a> - provides for an alternative
means of “merging” a single object into the <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a></p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="sqlalchemy.orm.session.Session.new">
<code class="descname">new</code><a class="headerlink" href="#sqlalchemy.orm.session.Session.new" title="Permalink to this definition">¶</a></dt>
<dd><p>The set of all instances marked as ‘new’ within this <code class="docutils literal notranslate"><span class="pre">Session</span></code>.</p>
</dd></dl>

<dl class="attribute">
<dt id="sqlalchemy.orm.session.Session.no_autoflush">
<code class="descname">no_autoflush</code><a class="headerlink" href="#sqlalchemy.orm.session.Session.no_autoflush" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a context manager that disables autoflush.</p>
<p>e.g.:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="n">session</span><span class="o">.</span><span class="n">no_autoflush</span><span class="p">:</span>

    <span class="n">some_object</span> <span class="o">=</span> <span class="n">SomeClass</span><span class="p">()</span>
    <span class="n">session</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">some_object</span><span class="p">)</span>
    <span class="c1"># won&#39;t autoflush</span>
    <span class="n">some_object</span><span class="o">.</span><span class="n">related_thing</span> <span class="o">=</span> <span class="n">session</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">SomeRelated</span><span class="p">)</span><span class="o">.</span><span class="n">first</span><span class="p">()</span></pre></div>
</div>
<p>Operations that proceed within the <code class="docutils literal notranslate"><span class="pre">with:</span></code> block
will not be subject to flushes occurring upon query
access.  This is useful when initializing a series
of objects which involve existing database queries,
where the uncompleted object should not yet be flushed.</p>
</dd></dl>

<dl class="classmethod">
<dt id="sqlalchemy.orm.session.Session.object_session">
<em class="property">classmethod </em><code class="descname">object_session</code><span class="sig-paren">(</span><em>instance</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.Session.object_session" title="Permalink to this definition">¶</a></dt>
<dd><div class="inherited-member docutils container">
<p><em>inherited from the</em> <code class="xref py py-meth docutils literal notranslate"><span class="pre">object_session()</span></code> <em>method of</em> <code class="xref py py-class docutils literal notranslate"><span class="pre">_SessionClassMethods</span></code></p>
</div>
<p>Return the <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> to which an object belongs.</p>
<p>This is an alias of <a class="reference internal" href="#sqlalchemy.orm.session.object_session" title="sqlalchemy.orm.session.object_session"><code class="xref py py-func docutils literal notranslate"><span class="pre">object_session()</span></code></a>.</p>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.orm.session.Session.prepare">
<code class="descname">prepare</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.Session.prepare" title="Permalink to this definition">¶</a></dt>
<dd><p>Prepare the current transaction in progress for two phase commit.</p>
<p>If no transaction is in progress, this method raises an
<a class="reference internal" href="../core/exceptions.html#sqlalchemy.exc.InvalidRequestError" title="sqlalchemy.exc.InvalidRequestError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">InvalidRequestError</span></code></a>.</p>
<p>Only root transactions of two phase sessions can be prepared. If the
current transaction is not such, an
<a class="reference internal" href="../core/exceptions.html#sqlalchemy.exc.InvalidRequestError" title="sqlalchemy.exc.InvalidRequestError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">InvalidRequestError</span></code></a> is raised.</p>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.orm.session.Session.prune">
<code class="descname">prune</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.Session.prune" title="Permalink to this definition">¶</a></dt>
<dd><p>Remove unreferenced instances cached in the identity map.</p>
<div class="deprecated">
<p><span class="versionmodified deprecated">Deprecated since version 0.7: </span>The <a class="reference internal" href="#sqlalchemy.orm.session.Session.prune" title="sqlalchemy.orm.session.Session.prune"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.prune()</span></code></a> method is deprecated along with <a class="reference internal" href="#sqlalchemy.orm.session.Session.params.weak_identity_map" title="sqlalchemy.orm.session.Session"><code class="xref py py-paramref docutils literal notranslate"><span class="pre">Session.weak_identity_map</span></code></a>.  This method will be removed in a future release.</p>
</div>
<p>Note that this method is only meaningful if “weak_identity_map” is set
to False.  The default weak identity map is self-pruning.</p>
<p>Removes any object in this Session’s identity map that is not
referenced in user code, modified, new or scheduled for deletion.
Returns the number of objects pruned.</p>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.orm.session.Session.query">
<code class="descname">query</code><span class="sig-paren">(</span><em>*entities</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.Session.query" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a new <a class="reference internal" href="query.html#sqlalchemy.orm.query.Query" title="sqlalchemy.orm.query.Query"><code class="xref py py-class docutils literal notranslate"><span class="pre">Query</span></code></a> object corresponding to this
<a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a>.</p>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.orm.session.Session.refresh">
<code class="descname">refresh</code><span class="sig-paren">(</span><em>instance</em>, <em>attribute_names=None</em>, <em>with_for_update=None</em>, <em>lockmode=None</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.Session.refresh" title="Permalink to this definition">¶</a></dt>
<dd><p>Expire and refresh the attributes on the given instance.</p>
<p>A query will be issued to the database and all attributes will be
refreshed with their current database value.</p>
<p>Lazy-loaded relational attributes will remain lazily loaded, so that
the instance-wide refresh operation will be followed immediately by
the lazy load of that attribute.</p>
<p>Eagerly-loaded relational attributes will eagerly load within the
single refresh operation.</p>
<p>Note that a highly isolated transaction will return the same values as
were previously read in that same transaction, regardless of changes
in database state outside of that transaction - usage of
<a class="reference internal" href="#sqlalchemy.orm.session.Session.refresh" title="sqlalchemy.orm.session.Session.refresh"><code class="xref py py-meth docutils literal notranslate"><span class="pre">refresh()</span></code></a> usually only makes sense if non-ORM SQL
statement were emitted in the ongoing transaction, or if autocommit
mode is turned on.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><span class="target" id="sqlalchemy.orm.session.Session.refresh.params.attribute_names"></span><strong>attribute_names</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.refresh.params.attribute_names">¶</a> – optional.  An iterable collection of
string attribute names indicating a subset of attributes to
be refreshed.</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.Session.refresh.params.with_for_update"></span><strong>with_for_update</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.refresh.params.with_for_update">¶</a> – <p>optional boolean <code class="docutils literal notranslate"><span class="pre">True</span></code> indicating FOR UPDATE
should be used, or may be a dictionary containing flags to
indicate a more specific set of FOR UPDATE flags for the SELECT;
flags should match the parameters of <a class="reference internal" href="query.html#sqlalchemy.orm.query.Query.with_for_update" title="sqlalchemy.orm.query.Query.with_for_update"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Query.with_for_update()</span></code></a>.
Supersedes the <a class="reference internal" href="#sqlalchemy.orm.session.Session.refresh.params.lockmode" title="sqlalchemy.orm.session.Session.refresh"><code class="xref py py-paramref docutils literal notranslate"><span class="pre">Session.refresh.lockmode</span></code></a> parameter.</p>
<div class="versionadded">
<p><span class="versionmodified added">New in version 1.2.</span></p>
</div>
</p></li>
<li><p><span class="target" id="sqlalchemy.orm.session.Session.refresh.params.lockmode"></span><strong>lockmode</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.session.Session.refresh.params.lockmode">¶</a> – Passed to the <a class="reference internal" href="query.html#sqlalchemy.orm.query.Query" title="sqlalchemy.orm.query.Query"><code class="xref py py-class docutils literal notranslate"><span class="pre">Query</span></code></a>
as used by <a class="reference internal" href="query.html#sqlalchemy.orm.query.Query.with_lockmode" title="sqlalchemy.orm.query.Query.with_lockmode"><code class="xref py py-meth docutils literal notranslate"><span class="pre">with_lockmode()</span></code></a>.
Superseded by <a class="reference internal" href="#sqlalchemy.orm.session.Session.refresh.params.with_for_update" title="sqlalchemy.orm.session.Session.refresh"><code class="xref py py-paramref docutils literal notranslate"><span class="pre">Session.refresh.with_for_update</span></code></a>.</p></li>
</ul>
</dd>
</dl>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="session_state_management.html#session-expire"><span class="std std-ref">Refreshing / Expiring</span></a> - introductory material</p>
<p><a class="reference internal" href="#sqlalchemy.orm.session.Session.expire" title="sqlalchemy.orm.session.Session.expire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.expire()</span></code></a></p>
<p><a class="reference internal" href="#sqlalchemy.orm.session.Session.expire_all" title="sqlalchemy.orm.session.Session.expire_all"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.expire_all()</span></code></a></p>
</div>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.orm.session.Session.rollback">
<code class="descname">rollback</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.Session.rollback" title="Permalink to this definition">¶</a></dt>
<dd><p>Rollback the current transaction in progress.</p>
<p>If no transaction is in progress, this method is a pass-through.</p>
<p>This method rolls back the current transaction or nested transaction
regardless of subtransactions being in effect.  All subtransactions up
to the first real transaction are closed.  Subtransactions occur when
<a class="reference internal" href="#sqlalchemy.orm.session.Session.begin" title="sqlalchemy.orm.session.Session.begin"><code class="xref py py-meth docutils literal notranslate"><span class="pre">begin()</span></code></a> is called multiple times.</p>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="session_basics.html#session-rollback"><span class="std std-ref">Rolling Back</span></a></p>
</div>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.orm.session.Session.scalar">
<code class="descname">scalar</code><span class="sig-paren">(</span><em>clause</em>, <em>params=None</em>, <em>mapper=None</em>, <em>bind=None</em>, <em>**kw</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.Session.scalar" title="Permalink to this definition">¶</a></dt>
<dd><p>Like <a class="reference internal" href="#sqlalchemy.orm.session.Session.execute" title="sqlalchemy.orm.session.Session.execute"><code class="xref py py-meth docutils literal notranslate"><span class="pre">execute()</span></code></a> but return a scalar result.</p>
</dd></dl>

<dl class="attribute">
<dt id="sqlalchemy.orm.session.Session.transaction">
<code class="descname">transaction</code><em class="property"> = None</em><a class="headerlink" href="#sqlalchemy.orm.session.Session.transaction" title="Permalink to this definition">¶</a></dt>
<dd><p>The current active or inactive <a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction" title="sqlalchemy.orm.session.SessionTransaction"><code class="xref py py-class docutils literal notranslate"><span class="pre">SessionTransaction</span></code></a>.</p>
</dd></dl>

</dd></dl>

<dl class="class">
<dt id="sqlalchemy.orm.session.SessionTransaction">
<em class="property">class </em><code class="descclassname">sqlalchemy.orm.session.</code><code class="descname">SessionTransaction</code><span class="sig-paren">(</span><em>session</em>, <em>parent=None</em>, <em>nested=False</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.SessionTransaction" title="Permalink to this definition">¶</a></dt>
<dd><p>A <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a>-level transaction.</p>
<p><a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction" title="sqlalchemy.orm.session.SessionTransaction"><code class="xref py py-class docutils literal notranslate"><span class="pre">SessionTransaction</span></code></a> is a mostly behind-the-scenes object
not normally referenced directly by application code.   It coordinates
among multiple <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Connection" title="sqlalchemy.engine.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a> objects, maintaining a database
transaction for each one individually, committing or rolling them
back all at once.   It also provides optional two-phase commit behavior
which can augment this coordination operation.</p>
<p>The <a class="reference internal" href="#sqlalchemy.orm.session.Session.transaction" title="sqlalchemy.orm.session.Session.transaction"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Session.transaction</span></code></a> attribute of <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a>
refers to the current <a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction" title="sqlalchemy.orm.session.SessionTransaction"><code class="xref py py-class docutils literal notranslate"><span class="pre">SessionTransaction</span></code></a> object in use, if any.
The <a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction.parent" title="sqlalchemy.orm.session.SessionTransaction.parent"><code class="xref py py-attr docutils literal notranslate"><span class="pre">SessionTransaction.parent</span></code></a> attribute refers to the parent
<a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction" title="sqlalchemy.orm.session.SessionTransaction"><code class="xref py py-class docutils literal notranslate"><span class="pre">SessionTransaction</span></code></a> in the stack of <a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction" title="sqlalchemy.orm.session.SessionTransaction"><code class="xref py py-class docutils literal notranslate"><span class="pre">SessionTransaction</span></code></a>
objects.  If this attribute is <code class="docutils literal notranslate"><span class="pre">None</span></code>, then this is the top of the stack.
If non-<code class="docutils literal notranslate"><span class="pre">None</span></code>, then this <a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction" title="sqlalchemy.orm.session.SessionTransaction"><code class="xref py py-class docutils literal notranslate"><span class="pre">SessionTransaction</span></code></a> refers either
to a so-called “subtransaction” or a “nested” transaction.  A
“subtransaction” is a scoping concept that demarcates an inner portion
of the outermost “real” transaction.  A nested transaction, which
is indicated when the <a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction.nested" title="sqlalchemy.orm.session.SessionTransaction.nested"><code class="xref py py-attr docutils literal notranslate"><span class="pre">SessionTransaction.nested</span></code></a>
attribute is also True, indicates that this <a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction" title="sqlalchemy.orm.session.SessionTransaction"><code class="xref py py-class docutils literal notranslate"><span class="pre">SessionTransaction</span></code></a>
corresponds to a SAVEPOINT.</p>
<p><strong>Life Cycle</strong></p>
<p>A <a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction" title="sqlalchemy.orm.session.SessionTransaction"><code class="xref py py-class docutils literal notranslate"><span class="pre">SessionTransaction</span></code></a> is associated with a <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a>
in its default mode of <code class="docutils literal notranslate"><span class="pre">autocommit=False</span></code> immediately, associated
with no database connections.  As the <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> is called upon
to emit SQL on behalf of various <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Engine</span></code></a> or <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Connection" title="sqlalchemy.engine.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a>
objects, a corresponding <a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Connection" title="sqlalchemy.engine.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a> and associated
<a class="reference internal" href="../core/connections.html#sqlalchemy.engine.Transaction" title="sqlalchemy.engine.Transaction"><code class="xref py py-class docutils literal notranslate"><span class="pre">Transaction</span></code></a> is added to a collection within the
<a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction" title="sqlalchemy.orm.session.SessionTransaction"><code class="xref py py-class docutils literal notranslate"><span class="pre">SessionTransaction</span></code></a> object, becoming one of the
connection/transaction pairs maintained by the
<a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction" title="sqlalchemy.orm.session.SessionTransaction"><code class="xref py py-class docutils literal notranslate"><span class="pre">SessionTransaction</span></code></a>.  The start of a <a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction" title="sqlalchemy.orm.session.SessionTransaction"><code class="xref py py-class docutils literal notranslate"><span class="pre">SessionTransaction</span></code></a>
can be tracked using the <a class="reference internal" href="events.html#sqlalchemy.orm.events.SessionEvents.after_transaction_create" title="sqlalchemy.orm.events.SessionEvents.after_transaction_create"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SessionEvents.after_transaction_create()</span></code></a>
event.</p>
<p>The lifespan of the <a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction" title="sqlalchemy.orm.session.SessionTransaction"><code class="xref py py-class docutils literal notranslate"><span class="pre">SessionTransaction</span></code></a> ends when the
<a class="reference internal" href="#sqlalchemy.orm.session.Session.commit" title="sqlalchemy.orm.session.Session.commit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.commit()</span></code></a>, <a class="reference internal" href="#sqlalchemy.orm.session.Session.rollback" title="sqlalchemy.orm.session.Session.rollback"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.rollback()</span></code></a> or
<a class="reference internal" href="#sqlalchemy.orm.session.Session.close" title="sqlalchemy.orm.session.Session.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.close()</span></code></a> methods are called.  At this point, the
<a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction" title="sqlalchemy.orm.session.SessionTransaction"><code class="xref py py-class docutils literal notranslate"><span class="pre">SessionTransaction</span></code></a> removes its association with its parent
<a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a>.   A <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> that is in <code class="docutils literal notranslate"><span class="pre">autocommit=False</span></code>
mode will create a new <a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction" title="sqlalchemy.orm.session.SessionTransaction"><code class="xref py py-class docutils literal notranslate"><span class="pre">SessionTransaction</span></code></a> to replace it
immediately, whereas a <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> that’s in <code class="docutils literal notranslate"><span class="pre">autocommit=True</span></code>
mode will remain without a <a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction" title="sqlalchemy.orm.session.SessionTransaction"><code class="xref py py-class docutils literal notranslate"><span class="pre">SessionTransaction</span></code></a> until the
<a class="reference internal" href="#sqlalchemy.orm.session.Session.begin" title="sqlalchemy.orm.session.Session.begin"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.begin()</span></code></a> method is called.  The end of a
<a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction" title="sqlalchemy.orm.session.SessionTransaction"><code class="xref py py-class docutils literal notranslate"><span class="pre">SessionTransaction</span></code></a> can be tracked using the
<a class="reference internal" href="events.html#sqlalchemy.orm.events.SessionEvents.after_transaction_end" title="sqlalchemy.orm.events.SessionEvents.after_transaction_end"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SessionEvents.after_transaction_end()</span></code></a> event.</p>
<p><strong>Nesting and Subtransactions</strong></p>
<p>Another detail of <a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction" title="sqlalchemy.orm.session.SessionTransaction"><code class="xref py py-class docutils literal notranslate"><span class="pre">SessionTransaction</span></code></a> behavior is that it is
capable of “nesting”.  This means that the <a class="reference internal" href="#sqlalchemy.orm.session.Session.begin" title="sqlalchemy.orm.session.Session.begin"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.begin()</span></code></a> method
can be called while an existing <a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction" title="sqlalchemy.orm.session.SessionTransaction"><code class="xref py py-class docutils literal notranslate"><span class="pre">SessionTransaction</span></code></a> is already
present, producing a new <a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction" title="sqlalchemy.orm.session.SessionTransaction"><code class="xref py py-class docutils literal notranslate"><span class="pre">SessionTransaction</span></code></a> that temporarily
replaces the parent <a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction" title="sqlalchemy.orm.session.SessionTransaction"><code class="xref py py-class docutils literal notranslate"><span class="pre">SessionTransaction</span></code></a>.   When a
<a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction" title="sqlalchemy.orm.session.SessionTransaction"><code class="xref py py-class docutils literal notranslate"><span class="pre">SessionTransaction</span></code></a> is produced as nested, it assigns itself to
the <a class="reference internal" href="#sqlalchemy.orm.session.Session.transaction" title="sqlalchemy.orm.session.Session.transaction"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Session.transaction</span></code></a> attribute, and it additionally will assign
the previous <a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction" title="sqlalchemy.orm.session.SessionTransaction"><code class="xref py py-class docutils literal notranslate"><span class="pre">SessionTransaction</span></code></a> to its <code class="xref py py-attr docutils literal notranslate"><span class="pre">Session.parent</span></code>
attribute.  The behavior is effectively a
stack, where <a class="reference internal" href="#sqlalchemy.orm.session.Session.transaction" title="sqlalchemy.orm.session.Session.transaction"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Session.transaction</span></code></a> refers to the current head of
the stack, and the <a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction.parent" title="sqlalchemy.orm.session.SessionTransaction.parent"><code class="xref py py-attr docutils literal notranslate"><span class="pre">SessionTransaction.parent</span></code></a> attribute allows
traversal up the stack until <a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction.parent" title="sqlalchemy.orm.session.SessionTransaction.parent"><code class="xref py py-attr docutils literal notranslate"><span class="pre">SessionTransaction.parent</span></code></a> is
<code class="docutils literal notranslate"><span class="pre">None</span></code>, indicating the top of the stack.</p>
<p>When the scope of <a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction" title="sqlalchemy.orm.session.SessionTransaction"><code class="xref py py-class docutils literal notranslate"><span class="pre">SessionTransaction</span></code></a> is ended via
<a class="reference internal" href="#sqlalchemy.orm.session.Session.commit" title="sqlalchemy.orm.session.Session.commit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.commit()</span></code></a> or <a class="reference internal" href="#sqlalchemy.orm.session.Session.rollback" title="sqlalchemy.orm.session.Session.rollback"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.rollback()</span></code></a>, it restores its
parent <a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction" title="sqlalchemy.orm.session.SessionTransaction"><code class="xref py py-class docutils literal notranslate"><span class="pre">SessionTransaction</span></code></a> back onto the
<a class="reference internal" href="#sqlalchemy.orm.session.Session.transaction" title="sqlalchemy.orm.session.Session.transaction"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Session.transaction</span></code></a> attribute.</p>
<p>The purpose of this stack is to allow nesting of
<a class="reference internal" href="#sqlalchemy.orm.session.Session.rollback" title="sqlalchemy.orm.session.Session.rollback"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.rollback()</span></code></a> or <a class="reference internal" href="#sqlalchemy.orm.session.Session.commit" title="sqlalchemy.orm.session.Session.commit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.commit()</span></code></a> calls in context
with various flavors of <a class="reference internal" href="#sqlalchemy.orm.session.Session.begin" title="sqlalchemy.orm.session.Session.begin"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.begin()</span></code></a>. This nesting behavior
applies to when <a class="reference internal" href="#sqlalchemy.orm.session.Session.begin_nested" title="sqlalchemy.orm.session.Session.begin_nested"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.begin_nested()</span></code></a> is used to emit a
SAVEPOINT transaction, and is also used to produce a so-called
“subtransaction” which allows a block of code to use a
begin/rollback/commit sequence regardless of whether or not its enclosing
code block has begun a transaction.  The <a class="reference internal" href="#sqlalchemy.orm.session.Session.flush" title="sqlalchemy.orm.session.Session.flush"><code class="xref py py-meth docutils literal notranslate"><span class="pre">flush()</span></code></a> method, whether
called explicitly or via autoflush, is the primary consumer of the
“subtransaction” feature, in that it wishes to guarantee that it works
within in a transaction block regardless of whether or not the
<a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> is in transactional mode when the method is called.</p>
<p>Note that the flush process that occurs within the “autoflush” feature
as well as when the <a class="reference internal" href="#sqlalchemy.orm.session.Session.flush" title="sqlalchemy.orm.session.Session.flush"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.flush()</span></code></a> method is used <strong>always</strong>
creates a <a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction" title="sqlalchemy.orm.session.SessionTransaction"><code class="xref py py-class docutils literal notranslate"><span class="pre">SessionTransaction</span></code></a> object.   This object is normally
a subtransaction, unless the <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> is in autocommit mode
and no transaction exists at all, in which case it’s the outermost
transaction.   Any event-handling logic or other inspection logic
needs to take into account whether a <a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction" title="sqlalchemy.orm.session.SessionTransaction"><code class="xref py py-class docutils literal notranslate"><span class="pre">SessionTransaction</span></code></a>
is the outermost transaction, a subtransaction, or a “nested” / SAVEPOINT
transaction.</p>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sqlalchemy.orm.session.Session.rollback" title="sqlalchemy.orm.session.Session.rollback"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.rollback()</span></code></a></p>
<p><a class="reference internal" href="#sqlalchemy.orm.session.Session.commit" title="sqlalchemy.orm.session.Session.commit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.commit()</span></code></a></p>
<p><a class="reference internal" href="#sqlalchemy.orm.session.Session.begin" title="sqlalchemy.orm.session.Session.begin"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.begin()</span></code></a></p>
<p><a class="reference internal" href="#sqlalchemy.orm.session.Session.begin_nested" title="sqlalchemy.orm.session.Session.begin_nested"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.begin_nested()</span></code></a></p>
<p><a class="reference internal" href="#sqlalchemy.orm.session.Session.is_active" title="sqlalchemy.orm.session.Session.is_active"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Session.is_active</span></code></a></p>
<p><a class="reference internal" href="events.html#sqlalchemy.orm.events.SessionEvents.after_transaction_create" title="sqlalchemy.orm.events.SessionEvents.after_transaction_create"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SessionEvents.after_transaction_create()</span></code></a></p>
<p><a class="reference internal" href="events.html#sqlalchemy.orm.events.SessionEvents.after_transaction_end" title="sqlalchemy.orm.events.SessionEvents.after_transaction_end"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SessionEvents.after_transaction_end()</span></code></a></p>
<p><a class="reference internal" href="events.html#sqlalchemy.orm.events.SessionEvents.after_commit" title="sqlalchemy.orm.events.SessionEvents.after_commit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SessionEvents.after_commit()</span></code></a></p>
<p><a class="reference internal" href="events.html#sqlalchemy.orm.events.SessionEvents.after_rollback" title="sqlalchemy.orm.events.SessionEvents.after_rollback"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SessionEvents.after_rollback()</span></code></a></p>
<p><a class="reference internal" href="events.html#sqlalchemy.orm.events.SessionEvents.after_soft_rollback" title="sqlalchemy.orm.events.SessionEvents.after_soft_rollback"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SessionEvents.after_soft_rollback()</span></code></a></p>
</div>
<dl class="attribute">
<dt id="sqlalchemy.orm.session.SessionTransaction.nested">
<code class="descname">nested</code><em class="property"> = False</em><a class="headerlink" href="#sqlalchemy.orm.session.SessionTransaction.nested" title="Permalink to this definition">¶</a></dt>
<dd><p>Indicates if this is a nested, or SAVEPOINT, transaction.</p>
<p>When <a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction.nested" title="sqlalchemy.orm.session.SessionTransaction.nested"><code class="xref py py-attr docutils literal notranslate"><span class="pre">SessionTransaction.nested</span></code></a> is True, it is expected
that <a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction.parent" title="sqlalchemy.orm.session.SessionTransaction.parent"><code class="xref py py-attr docutils literal notranslate"><span class="pre">SessionTransaction.parent</span></code></a> will be True as well.</p>
</dd></dl>

<dl class="attribute">
<dt id="sqlalchemy.orm.session.SessionTransaction.parent">
<code class="descname">parent</code><a class="headerlink" href="#sqlalchemy.orm.session.SessionTransaction.parent" title="Permalink to this definition">¶</a></dt>
<dd><p>The parent <a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction" title="sqlalchemy.orm.session.SessionTransaction"><code class="xref py py-class docutils literal notranslate"><span class="pre">SessionTransaction</span></code></a> of this
<a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction" title="sqlalchemy.orm.session.SessionTransaction"><code class="xref py py-class docutils literal notranslate"><span class="pre">SessionTransaction</span></code></a>.</p>
<p>If this attribute is <code class="docutils literal notranslate"><span class="pre">None</span></code>, indicates this
<a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction" title="sqlalchemy.orm.session.SessionTransaction"><code class="xref py py-class docutils literal notranslate"><span class="pre">SessionTransaction</span></code></a> is at the top of the stack, and
corresponds to a real “COMMIT”/”ROLLBACK”
block.  If non-<code class="docutils literal notranslate"><span class="pre">None</span></code>, then this is either a “subtransaction”
or a “nested” / SAVEPOINT transaction.  If the
<a class="reference internal" href="#sqlalchemy.orm.session.SessionTransaction.nested" title="sqlalchemy.orm.session.SessionTransaction.nested"><code class="xref py py-attr docutils literal notranslate"><span class="pre">SessionTransaction.nested</span></code></a> attribute is <code class="docutils literal notranslate"><span class="pre">True</span></code>, then
this is a SAVEPOINT, and if <code class="docutils literal notranslate"><span class="pre">False</span></code>, indicates this a subtransaction.</p>
<div class="versionadded">
<p><span class="versionmodified added">New in version 1.0.16: </span>- use ._parent for previous versions</p>
</div>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="session-utilities">
<h2>Session Utilities<a class="headerlink" href="#session-utilities" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="sqlalchemy.orm.session.make_transient">
<code class="descclassname">sqlalchemy.orm.session.</code><code class="descname">make_transient</code><span class="sig-paren">(</span><em>instance</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.make_transient" title="Permalink to this definition">¶</a></dt>
<dd><p>Alter the state of the given instance so that it is <a class="reference internal" href="../glossary.html#term-transient"><span class="xref std std-term">transient</span></a>.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p><a class="reference internal" href="#sqlalchemy.orm.session.make_transient" title="sqlalchemy.orm.session.make_transient"><code class="xref py py-func docutils literal notranslate"><span class="pre">make_transient()</span></code></a> is a special-case function for
advanced use cases only.</p>
</div>
<p>The given mapped instance is assumed to be in the <a class="reference internal" href="../glossary.html#term-persistent"><span class="xref std std-term">persistent</span></a> or
<a class="reference internal" href="../glossary.html#term-detached"><span class="xref std std-term">detached</span></a> state.   The function will remove its association with any
<a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> as well as its <a class="reference internal" href="internals.html#sqlalchemy.orm.state.InstanceState.identity" title="sqlalchemy.orm.state.InstanceState.identity"><code class="xref py py-attr docutils literal notranslate"><span class="pre">InstanceState.identity</span></code></a>. The
effect is that the object will behave as though it were newly constructed,
except retaining any attribute / collection values that were loaded at the
time of the call.   The <a class="reference internal" href="internals.html#sqlalchemy.orm.state.InstanceState.deleted" title="sqlalchemy.orm.state.InstanceState.deleted"><code class="xref py py-attr docutils literal notranslate"><span class="pre">InstanceState.deleted</span></code></a> flag is also reset
if this object had been deleted as a result of using
<a class="reference internal" href="#sqlalchemy.orm.session.Session.delete" title="sqlalchemy.orm.session.Session.delete"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.delete()</span></code></a>.</p>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p><a class="reference internal" href="#sqlalchemy.orm.session.make_transient" title="sqlalchemy.orm.session.make_transient"><code class="xref py py-func docutils literal notranslate"><span class="pre">make_transient()</span></code></a> does <strong>not</strong> “unexpire” or otherwise eagerly
load ORM-mapped attributes that are not currently loaded at the time
the function is called.   This includes attributes which:</p>
<ul class="simple">
<li><p>were expired via <a class="reference internal" href="#sqlalchemy.orm.session.Session.expire" title="sqlalchemy.orm.session.Session.expire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.expire()</span></code></a></p></li>
<li><p>were expired as the natural effect of committing a session
transaction, e.g. <a class="reference internal" href="#sqlalchemy.orm.session.Session.commit" title="sqlalchemy.orm.session.Session.commit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.commit()</span></code></a></p></li>
<li><p>are normally <a class="reference internal" href="../glossary.html#term-lazy-loaded"><span class="xref std std-term">lazy loaded</span></a> but are not currently loaded</p></li>
<li><p>are “deferred” via <a class="reference internal" href="loading_columns.html#deferred"><span class="std std-ref">Deferred Column Loading</span></a> and are not yet loaded</p></li>
<li><p>were not present in the query which loaded this object, such as that
which is common in joined table inheritance and other scenarios.</p></li>
</ul>
<p>After <a class="reference internal" href="#sqlalchemy.orm.session.make_transient" title="sqlalchemy.orm.session.make_transient"><code class="xref py py-func docutils literal notranslate"><span class="pre">make_transient()</span></code></a> is called, unloaded attributes such
as those above will normally resolve to the value <code class="docutils literal notranslate"><span class="pre">None</span></code> when
accessed, or an empty collection for a collection-oriented attribute.
As the object is transient and un-associated with any database
identity, it will no longer retrieve these values.</p>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sqlalchemy.orm.session.make_transient_to_detached" title="sqlalchemy.orm.session.make_transient_to_detached"><code class="xref py py-func docutils literal notranslate"><span class="pre">make_transient_to_detached()</span></code></a></p>
</div>
</dd></dl>

<dl class="function">
<dt id="sqlalchemy.orm.session.make_transient_to_detached">
<code class="descclassname">sqlalchemy.orm.session.</code><code class="descname">make_transient_to_detached</code><span class="sig-paren">(</span><em>instance</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.make_transient_to_detached" title="Permalink to this definition">¶</a></dt>
<dd><p>Make the given transient instance <a class="reference internal" href="../glossary.html#term-detached"><span class="xref std std-term">detached</span></a>.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p><a class="reference internal" href="#sqlalchemy.orm.session.make_transient_to_detached" title="sqlalchemy.orm.session.make_transient_to_detached"><code class="xref py py-func docutils literal notranslate"><span class="pre">make_transient_to_detached()</span></code></a> is a special-case function for
advanced use cases only.</p>
</div>
<p>All attribute history on the given instance
will be reset as though the instance were freshly loaded
from a query.  Missing attributes will be marked as expired.
The primary key attributes of the object, which are required, will be made
into the “key” of the instance.</p>
<p>The object can then be added to a session, or merged
possibly with the load=False flag, at which point it will look
as if it were loaded that way, without emitting SQL.</p>
<p>This is a special use case function that differs from a normal
call to <a class="reference internal" href="#sqlalchemy.orm.session.Session.merge" title="sqlalchemy.orm.session.Session.merge"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.merge()</span></code></a> in that a given persistent state
can be manufactured without any SQL calls.</p>
<div class="versionadded">
<p><span class="versionmodified added">New in version 0.9.5.</span></p>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sqlalchemy.orm.session.make_transient" title="sqlalchemy.orm.session.make_transient"><code class="xref py py-func docutils literal notranslate"><span class="pre">make_transient()</span></code></a></p>
<p><a class="reference internal" href="#sqlalchemy.orm.session.Session.enable_relationship_loading" title="sqlalchemy.orm.session.Session.enable_relationship_loading"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Session.enable_relationship_loading()</span></code></a></p>
</div>
</dd></dl>

<dl class="function">
<dt id="sqlalchemy.orm.session.object_session">
<code class="descclassname">sqlalchemy.orm.session.</code><code class="descname">object_session</code><span class="sig-paren">(</span><em>instance</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.session.object_session" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> to which the given instance belongs.</p>
<p>This is essentially the same as the <a class="reference internal" href="internals.html#sqlalchemy.orm.state.InstanceState.session" title="sqlalchemy.orm.state.InstanceState.session"><code class="xref py py-attr docutils literal notranslate"><span class="pre">InstanceState.session</span></code></a>
accessor.  See that attribute for details.</p>
</dd></dl>

<dl class="function">
<dt id="sqlalchemy.orm.util.was_deleted">
<code class="descclassname">sqlalchemy.orm.util.</code><code class="descname">was_deleted</code><span class="sig-paren">(</span><em>object_</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.util.was_deleted" title="Permalink to this definition">¶</a></dt>
<dd><p>Return True if the given object was deleted
within a session flush.</p>
<p>This is regardless of whether or not the object is
persistent or detached.</p>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="internals.html#sqlalchemy.orm.state.InstanceState.was_deleted" title="sqlalchemy.orm.state.InstanceState.was_deleted"><code class="xref py py-attr docutils literal notranslate"><span class="pre">InstanceState.was_deleted</span></code></a></p>
</div>
</dd></dl>

</div>
<div class="section" id="attribute-and-state-management-utilities">
<h2>Attribute and State Management Utilities<a class="headerlink" href="#attribute-and-state-management-utilities" title="Permalink to this headline">¶</a></h2>
<p>These functions are provided by the SQLAlchemy attribute
instrumentation API to provide a detailed interface for dealing
with instances, attribute values, and history.  Some of them
are useful when constructing event listener functions, such as
those described in <a class="reference internal" href="events.html"><span class="doc">ORM Events</span></a>.</p>
<dl class="function">
<dt id="sqlalchemy.orm.util.object_state">
<code class="descclassname">sqlalchemy.orm.util.</code><code class="descname">object_state</code><span class="sig-paren">(</span><em>instance</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.util.object_state" title="Permalink to this definition">¶</a></dt>
<dd><p>Given an object, return the <a class="reference internal" href="internals.html#sqlalchemy.orm.state.InstanceState" title="sqlalchemy.orm.state.InstanceState"><code class="xref py py-class docutils literal notranslate"><span class="pre">InstanceState</span></code></a>
associated with the object.</p>
<p>Raises <a class="reference internal" href="exceptions.html#sqlalchemy.orm.exc.UnmappedInstanceError" title="sqlalchemy.orm.exc.UnmappedInstanceError"><code class="xref py py-class docutils literal notranslate"><span class="pre">sqlalchemy.orm.exc.UnmappedInstanceError</span></code></a>
if no mapping is configured.</p>
<p>Equivalent functionality is available via the <a class="reference internal" href="../core/inspection.html#sqlalchemy.inspection.inspect" title="sqlalchemy.inspection.inspect"><code class="xref py py-func docutils literal notranslate"><span class="pre">inspect()</span></code></a>
function as:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">inspect</span><span class="p">(</span><span class="n">instance</span><span class="p">)</span></pre></div>
</div>
<p>Using the inspection system will raise
<a class="reference internal" href="../core/exceptions.html#sqlalchemy.exc.NoInspectionAvailable" title="sqlalchemy.exc.NoInspectionAvailable"><code class="xref py py-class docutils literal notranslate"><span class="pre">sqlalchemy.exc.NoInspectionAvailable</span></code></a> if the instance is
not part of a mapping.</p>
</dd></dl>

<dl class="function">
<dt id="sqlalchemy.orm.attributes.del_attribute">
<code class="descclassname">sqlalchemy.orm.attributes.</code><code class="descname">del_attribute</code><span class="sig-paren">(</span><em>instance</em>, <em>key</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.attributes.del_attribute" title="Permalink to this definition">¶</a></dt>
<dd><p>Delete the value of an attribute, firing history events.</p>
<p>This function may be used regardless of instrumentation
applied directly to the class, i.e. no descriptors are required.
Custom attribute management schemes will need to make usage
of this method to establish attribute state as understood
by SQLAlchemy.</p>
</dd></dl>

<dl class="function">
<dt id="sqlalchemy.orm.attributes.get_attribute">
<code class="descclassname">sqlalchemy.orm.attributes.</code><code class="descname">get_attribute</code><span class="sig-paren">(</span><em>instance</em>, <em>key</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.attributes.get_attribute" title="Permalink to this definition">¶</a></dt>
<dd><p>Get the value of an attribute, firing any callables required.</p>
<p>This function may be used regardless of instrumentation
applied directly to the class, i.e. no descriptors are required.
Custom attribute management schemes will need to make usage
of this method to make usage of attribute state as understood
by SQLAlchemy.</p>
</dd></dl>

<dl class="function">
<dt id="sqlalchemy.orm.attributes.get_history">
<code class="descclassname">sqlalchemy.orm.attributes.</code><code class="descname">get_history</code><span class="sig-paren">(</span><em>obj</em>, <em>key</em>, <em>passive=symbol('PASSIVE_OFF')</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.attributes.get_history" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a <a class="reference internal" href="#sqlalchemy.orm.attributes.History" title="sqlalchemy.orm.attributes.History"><code class="xref py py-class docutils literal notranslate"><span class="pre">History</span></code></a> record for the given object
and attribute key.</p>
<p>This is the <strong>pre-flush</strong> history for a given attribute, which is
reset each time the <a class="reference internal" href="#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> flushes changes to the
current database transaction.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Prefer to use the <a class="reference internal" href="internals.html#sqlalchemy.orm.state.AttributeState.history" title="sqlalchemy.orm.state.AttributeState.history"><code class="xref py py-attr docutils literal notranslate"><span class="pre">AttributeState.history</span></code></a> and
<a class="reference internal" href="internals.html#sqlalchemy.orm.state.AttributeState.load_history" title="sqlalchemy.orm.state.AttributeState.load_history"><code class="xref py py-meth docutils literal notranslate"><span class="pre">AttributeState.load_history()</span></code></a> accessors to retrieve the
<a class="reference internal" href="#sqlalchemy.orm.attributes.History" title="sqlalchemy.orm.attributes.History"><code class="xref py py-class docutils literal notranslate"><span class="pre">History</span></code></a> for instance attributes.</p>
</div>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><span class="target" id="sqlalchemy.orm.attributes.get_history.params.obj"></span><strong>obj</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.attributes.get_history.params.obj">¶</a> – an object whose class is instrumented by the
attributes package.</p></li>
<li><p><span class="target" id="sqlalchemy.orm.attributes.get_history.params.key"></span><strong>key</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.attributes.get_history.params.key">¶</a> – string attribute name.</p></li>
<li><p><span class="target" id="sqlalchemy.orm.attributes.get_history.params.passive"></span><strong>passive</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.attributes.get_history.params.passive">¶</a> – indicates loading behavior for the attribute
if the value is not already present.   This is a
bitflag attribute, which defaults to the symbol
<code class="xref py py-attr docutils literal notranslate"><span class="pre">PASSIVE_OFF</span></code> indicating all necessary SQL
should be emitted.</p></li>
</ul>
</dd>
</dl>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="internals.html#sqlalchemy.orm.state.AttributeState.history" title="sqlalchemy.orm.state.AttributeState.history"><code class="xref py py-attr docutils literal notranslate"><span class="pre">AttributeState.history</span></code></a></p>
<p><a class="reference internal" href="internals.html#sqlalchemy.orm.state.AttributeState.load_history" title="sqlalchemy.orm.state.AttributeState.load_history"><code class="xref py py-meth docutils literal notranslate"><span class="pre">AttributeState.load_history()</span></code></a> - retrieve history
using loader callables if the value is not locally present.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="sqlalchemy.orm.attributes.init_collection">
<code class="descclassname">sqlalchemy.orm.attributes.</code><code class="descname">init_collection</code><span class="sig-paren">(</span><em>obj</em>, <em>key</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.attributes.init_collection" title="Permalink to this definition">¶</a></dt>
<dd><p>Initialize a collection attribute and return the collection adapter.</p>
<p>This function is used to provide direct access to collection internals
for a previously unloaded attribute.  e.g.:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">collection_adapter</span> <span class="o">=</span> <span class="n">init_collection</span><span class="p">(</span><span class="n">someobject</span><span class="p">,</span> <span class="s1">&#39;elements&#39;</span><span class="p">)</span>
<span class="k">for</span> <span class="n">elem</span> <span class="ow">in</span> <span class="n">values</span><span class="p">:</span>
    <span class="n">collection_adapter</span><span class="o">.</span><span class="n">append_without_event</span><span class="p">(</span><span class="n">elem</span><span class="p">)</span></pre></div>
</div>
<p>For an easier way to do the above, see
<a class="reference internal" href="#sqlalchemy.orm.attributes.set_committed_value" title="sqlalchemy.orm.attributes.set_committed_value"><code class="xref py py-func docutils literal notranslate"><span class="pre">set_committed_value()</span></code></a>.</p>
<p>obj is an instrumented object instance.  An InstanceState
is accepted directly for backwards compatibility but
this usage is deprecated.</p>
</dd></dl>

<dl class="function">
<dt id="sqlalchemy.orm.attributes.flag_modified">
<code class="descclassname">sqlalchemy.orm.attributes.</code><code class="descname">flag_modified</code><span class="sig-paren">(</span><em>instance</em>, <em>key</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.attributes.flag_modified" title="Permalink to this definition">¶</a></dt>
<dd><p>Mark an attribute on an instance as ‘modified’.</p>
<p>This sets the ‘modified’ flag on the instance and
establishes an unconditional change event for the given attribute.
The attribute must have a value present, else an
<a class="reference internal" href="../core/exceptions.html#sqlalchemy.exc.InvalidRequestError" title="sqlalchemy.exc.InvalidRequestError"><code class="xref py py-class docutils literal notranslate"><span class="pre">InvalidRequestError</span></code></a> is raised.</p>
<p>To mark an object “dirty” without referring to any specific attribute
so that it is considered within a flush, use the
<a class="reference internal" href="#sqlalchemy.orm.attributes.flag_dirty" title="sqlalchemy.orm.attributes.flag_dirty"><code class="xref py py-func docutils literal notranslate"><span class="pre">attributes.flag_dirty()</span></code></a> call.</p>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sqlalchemy.orm.attributes.flag_dirty" title="sqlalchemy.orm.attributes.flag_dirty"><code class="xref py py-func docutils literal notranslate"><span class="pre">attributes.flag_dirty()</span></code></a></p>
</div>
</dd></dl>

<dl class="function">
<dt id="sqlalchemy.orm.attributes.flag_dirty">
<code class="descclassname">sqlalchemy.orm.attributes.</code><code class="descname">flag_dirty</code><span class="sig-paren">(</span><em>instance</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.attributes.flag_dirty" title="Permalink to this definition">¶</a></dt>
<dd><p>Mark an instance as ‘dirty’ without any specific attribute mentioned.</p>
<p>This is a special operation that will allow the object to travel through
the flush process for interception by events such as
<a class="reference internal" href="events.html#sqlalchemy.orm.events.SessionEvents.before_flush" title="sqlalchemy.orm.events.SessionEvents.before_flush"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SessionEvents.before_flush()</span></code></a>.   Note that no SQL will be emitted in
the flush process for an object that has no changes, even if marked dirty
via this method.  However, a <a class="reference internal" href="events.html#sqlalchemy.orm.events.SessionEvents.before_flush" title="sqlalchemy.orm.events.SessionEvents.before_flush"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SessionEvents.before_flush()</span></code></a> handler
will be able to see the object in the <a class="reference internal" href="#sqlalchemy.orm.session.Session.dirty" title="sqlalchemy.orm.session.Session.dirty"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Session.dirty</span></code></a> collection and
may establish changes on it, which will then be included in the SQL
emitted.</p>
<div class="versionadded">
<p><span class="versionmodified added">New in version 1.2.</span></p>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sqlalchemy.orm.attributes.flag_modified" title="sqlalchemy.orm.attributes.flag_modified"><code class="xref py py-func docutils literal notranslate"><span class="pre">attributes.flag_modified()</span></code></a></p>
</div>
</dd></dl>

<dl class="function">
<dt id="sqlalchemy.orm.attributes.instance_state">
<code class="descclassname">sqlalchemy.orm.attributes.</code><code class="descname">instance_state</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.attributes.instance_state" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the <a class="reference internal" href="internals.html#sqlalchemy.orm.state.InstanceState" title="sqlalchemy.orm.state.InstanceState"><code class="xref py py-class docutils literal notranslate"><span class="pre">InstanceState</span></code></a> for a given
mapped object.</p>
<p>This function is the internal version
of <a class="reference internal" href="#sqlalchemy.orm.util.object_state" title="sqlalchemy.orm.util.object_state"><code class="xref py py-func docutils literal notranslate"><span class="pre">object_state()</span></code></a>.   The
<a class="reference internal" href="#sqlalchemy.orm.util.object_state" title="sqlalchemy.orm.util.object_state"><code class="xref py py-func docutils literal notranslate"><span class="pre">object_state()</span></code></a> and/or the
<a class="reference internal" href="../core/inspection.html#sqlalchemy.inspection.inspect" title="sqlalchemy.inspection.inspect"><code class="xref py py-func docutils literal notranslate"><span class="pre">inspect()</span></code></a> function is preferred here
as they each emit an informative exception
if the given object is not mapped.</p>
</dd></dl>

<dl class="function">
<dt id="sqlalchemy.orm.instrumentation.is_instrumented">
<code class="descclassname">sqlalchemy.orm.instrumentation.</code><code class="descname">is_instrumented</code><span class="sig-paren">(</span><em>instance</em>, <em>key</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.instrumentation.is_instrumented" title="Permalink to this definition">¶</a></dt>
<dd><p>Return True if the given attribute on the given instance is
instrumented by the attributes package.</p>
<p>This function may be used regardless of instrumentation
applied directly to the class, i.e. no descriptors are required.</p>
</dd></dl>

<dl class="function">
<dt id="sqlalchemy.orm.attributes.set_attribute">
<code class="descclassname">sqlalchemy.orm.attributes.</code><code class="descname">set_attribute</code><span class="sig-paren">(</span><em>instance</em>, <em>key</em>, <em>value</em>, <em>initiator=None</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.attributes.set_attribute" title="Permalink to this definition">¶</a></dt>
<dd><p>Set the value of an attribute, firing history events.</p>
<p>This function may be used regardless of instrumentation
applied directly to the class, i.e. no descriptors are required.
Custom attribute management schemes will need to make usage
of this method to establish attribute state as understood
by SQLAlchemy.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><span class="target" id="sqlalchemy.orm.attributes.set_attribute.params.instance"></span><strong>instance</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.attributes.set_attribute.params.instance">¶</a> – the object that will be modified</p></li>
<li><p><span class="target" id="sqlalchemy.orm.attributes.set_attribute.params.key"></span><strong>key</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.attributes.set_attribute.params.key">¶</a> – string name of the attribute</p></li>
<li><p><span class="target" id="sqlalchemy.orm.attributes.set_attribute.params.value"></span><strong>value</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.attributes.set_attribute.params.value">¶</a> – value to assign</p></li>
<li><p><span class="target" id="sqlalchemy.orm.attributes.set_attribute.params.initiator"></span><strong>initiator</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.orm.attributes.set_attribute.params.initiator">¶</a> – <p>an instance of <a class="reference internal" href="internals.html#sqlalchemy.orm.attributes.Event" title="sqlalchemy.orm.attributes.Event"><code class="xref py py-class docutils literal notranslate"><span class="pre">Event</span></code></a> that would have
been propagated from a previous event listener.  This argument
is used when the <a class="reference internal" href="#sqlalchemy.orm.attributes.set_attribute" title="sqlalchemy.orm.attributes.set_attribute"><code class="xref py py-func docutils literal notranslate"><span class="pre">set_attribute()</span></code></a> function is being used within
an existing event listening function where an <a class="reference internal" href="internals.html#sqlalchemy.orm.attributes.Event" title="sqlalchemy.orm.attributes.Event"><code class="xref py py-class docutils literal notranslate"><span class="pre">Event</span></code></a> object
is being supplied; the object may be used to track the origin of the
chain of events.</p>
<div class="versionadded">
<p><span class="versionmodified added">New in version 1.2.3.</span></p>
</div>
</p></li>
</ul>
</dd>
</dl>
</dd></dl>

<dl class="function">
<dt id="sqlalchemy.orm.attributes.set_committed_value">
<code class="descclassname">sqlalchemy.orm.attributes.</code><code class="descname">set_committed_value</code><span class="sig-paren">(</span><em>instance</em>, <em>key</em>, <em>value</em><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.attributes.set_committed_value" title="Permalink to this definition">¶</a></dt>
<dd><p>Set the value of an attribute with no history events.</p>
<p>Cancels any previous history present.  The value should be
a scalar value for scalar-holding attributes, or
an iterable for any collection-holding attribute.</p>
<p>This is the same underlying method used when a lazy loader
fires off and loads additional data from the database.
In particular, this method can be used by application code
which has loaded additional attributes or collections through
separate queries, which can then be attached to an instance
as though it were part of its original loaded state.</p>
</dd></dl>

<dl class="class">
<dt id="sqlalchemy.orm.attributes.History">
<em class="property">class </em><code class="descclassname">sqlalchemy.orm.attributes.</code><code class="descname">History</code><a class="headerlink" href="#sqlalchemy.orm.attributes.History" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <a class="reference internal" href="#sqlalchemy.orm.attributes.History" title="sqlalchemy.orm.attributes.History"><code class="xref py py-class docutils literal notranslate"><span class="pre">sqlalchemy.orm.attributes.History</span></code></a></p>
<p>A 3-tuple of added, unchanged and deleted values,
representing the changes which have occurred on an instrumented
attribute.</p>
<p>The easiest way to get a <a class="reference internal" href="#sqlalchemy.orm.attributes.History" title="sqlalchemy.orm.attributes.History"><code class="xref py py-class docutils literal notranslate"><span class="pre">History</span></code></a> object for a particular
attribute on an object is to use the <a class="reference internal" href="../core/inspection.html#sqlalchemy.inspection.inspect" title="sqlalchemy.inspection.inspect"><code class="xref py py-func docutils literal notranslate"><span class="pre">inspect()</span></code></a> function:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">sqlalchemy</span> <span class="k">import</span> <span class="n">inspect</span>

<span class="n">hist</span> <span class="o">=</span> <span class="n">inspect</span><span class="p">(</span><span class="n">myobject</span><span class="p">)</span><span class="o">.</span><span class="n">attrs</span><span class="o">.</span><span class="n">myattribute</span><span class="o">.</span><span class="n">history</span></pre></div>
</div>
<p>Each tuple member is an iterable sequence:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">added</span></code> - the collection of items added to the attribute (the first
tuple element).</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">unchanged</span></code> - the collection of items that have not changed on the
attribute (the second tuple element).</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">deleted</span></code> - the collection of items that have been removed from the
attribute (the third tuple element).</p></li>
</ul>
<dl class="method">
<dt id="sqlalchemy.orm.attributes.History.empty">
<code class="descname">empty</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.attributes.History.empty" title="Permalink to this definition">¶</a></dt>
<dd><p>Return True if this <a class="reference internal" href="#sqlalchemy.orm.attributes.History" title="sqlalchemy.orm.attributes.History"><code class="xref py py-class docutils literal notranslate"><span class="pre">History</span></code></a> has no changes
and no existing, unchanged state.</p>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.orm.attributes.History.has_changes">
<code class="descname">has_changes</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.attributes.History.has_changes" title="Permalink to this definition">¶</a></dt>
<dd><p>Return True if this <a class="reference internal" href="#sqlalchemy.orm.attributes.History" title="sqlalchemy.orm.attributes.History"><code class="xref py py-class docutils literal notranslate"><span class="pre">History</span></code></a> has changes.</p>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.orm.attributes.History.non_added">
<code class="descname">non_added</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.attributes.History.non_added" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a collection of unchanged + deleted.</p>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.orm.attributes.History.non_deleted">
<code class="descname">non_deleted</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.attributes.History.non_deleted" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a collection of added + unchanged.</p>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.orm.attributes.History.sum">
<code class="descname">sum</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#sqlalchemy.orm.attributes.History.sum" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a collection of added + unchanged + deleted.</p>
</dd></dl>

</dd></dl>

</div>
</div>

    </div>

</div>

<div id="docs-bottom-navigation" class="docs-navigation-links, withsidebar">
        Previous:
        <a href="session_events.html" title="previous chapter">Tracking Object and Session Changes with Events</a>
        Next:
        <a href="extending.html" title="next chapter">Events and Internals</a>

    <div id="docs-copyright">
        &copy; <a href="../copyright.html">Copyright</a> 2007-2019, the SQLAlchemy authors and contributors.
        Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 2.0.1.
    </div>
</div>

</div>



        
        

    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
          URL_ROOT:    '../',
          VERSION:     '1.2.19',
          COLLAPSE_MODINDEX: false,
          FILE_SUFFIX: '.html'
      };
    </script>

    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>

    <!-- begin iterate through sphinx environment script_files -->
        <script type="text/javascript" src="../_static/jquery.js"></script>
        <script type="text/javascript" src="../_static/underscore.js"></script>
        <script type="text/javascript" src="../_static/doctools.js"></script>
        <script type="text/javascript" src="../_static/language_data.js"></script>
    <!-- end iterate through sphinx environment script_files -->

    <script type="text/javascript" src="../_static/detectmobile.js"></script>
    <script type="text/javascript" src="../_static/init.js"></script>


    </body>
</html>