<p dir="auto">If you query a model (e.g. with <code class="notranslate">session.query</code>) and filter on it, then join to a second model with <code class="notranslate">.join</code>, then use <code class="notranslate">.with_entities</code> to select that second model (the one you just joined to), SQLAlchemy will include the <em>second</em> model in the <code class="notranslate">FROM</code> clause. This can cause issues later in the query. Version 1.3 did not include that second model in <code class="notranslate">FROM</code>.</p>
<h2 dir="auto">Example</h2>
<p dir="auto">If you run this code:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import sqlalchemy as sa
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

DeclarativeBase = declarative_base()
Session = sessionmaker()
session = Session()


class First(DeclarativeBase):
    __tablename__ = &quot;first&quot;

    pk = sa.Column(sa.Integer, primary_key=True)


class Mapper(DeclarativeBase):
    __tablename__ = &quot;mapper&quot;

    pk = sa.Column(sa.Integer, primary_key=True)
    first_pk = sa.Column(sa.Integer, sa.ForeignKey(&quot;first.pk&quot;), nullable=False)
    second_pk = sa.Column(sa.Integer, sa.ForeignKey(&quot;second.pk&quot;), nullable=False)


class Second(DeclarativeBase):
    __tablename__ = &quot;second&quot;

    pk = sa.Column(sa.Integer, primary_key=True)


query = session.query(First).filter(First.pk != 1).join(
    Mapper,
    Mapper.first_pk == First.pk,
).join(
    Second,
    Second.pk == Mapper.second_pk,
).with_entities(Second)

if __name__ == &quot;__main__&quot;:
    print(query.statement)"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">sqlalchemy</span> <span class="pl-k">as</span> <span class="pl-s1">sa</span>
<span class="pl-k">from</span> <span class="pl-s1">sqlalchemy</span>.<span class="pl-s1">ext</span>.<span class="pl-s1">declarative</span> <span class="pl-k">import</span> <span class="pl-s1">declarative_base</span>
<span class="pl-k">from</span> <span class="pl-s1">sqlalchemy</span>.<span class="pl-s1">orm</span> <span class="pl-k">import</span> <span class="pl-s1">sessionmaker</span>

<span class="pl-v">DeclarativeBase</span> <span class="pl-c1">=</span> <span class="pl-en">declarative_base</span>()
<span class="pl-v">Session</span> <span class="pl-c1">=</span> <span class="pl-en">sessionmaker</span>()
<span class="pl-s1">session</span> <span class="pl-c1">=</span> <span class="pl-v">Session</span>()


<span class="pl-k">class</span> <span class="pl-v">First</span>(<span class="pl-v">DeclarativeBase</span>):
    <span class="pl-s1">__tablename__</span> <span class="pl-c1">=</span> <span class="pl-s">"first"</span>

    <span class="pl-s1">pk</span> <span class="pl-c1">=</span> <span class="pl-s1">sa</span>.<span class="pl-v">Column</span>(<span class="pl-s1">sa</span>.<span class="pl-v">Integer</span>, <span class="pl-s1">primary_key</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)


<span class="pl-k">class</span> <span class="pl-v">Mapper</span>(<span class="pl-v">DeclarativeBase</span>):
    <span class="pl-s1">__tablename__</span> <span class="pl-c1">=</span> <span class="pl-s">"mapper"</span>

    <span class="pl-s1">pk</span> <span class="pl-c1">=</span> <span class="pl-s1">sa</span>.<span class="pl-v">Column</span>(<span class="pl-s1">sa</span>.<span class="pl-v">Integer</span>, <span class="pl-s1">primary_key</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)
    <span class="pl-s1">first_pk</span> <span class="pl-c1">=</span> <span class="pl-s1">sa</span>.<span class="pl-v">Column</span>(<span class="pl-s1">sa</span>.<span class="pl-v">Integer</span>, <span class="pl-s1">sa</span>.<span class="pl-v">ForeignKey</span>(<span class="pl-s">"first.pk"</span>), <span class="pl-s1">nullable</span><span class="pl-c1">=</span><span class="pl-c1">False</span>)
    <span class="pl-s1">second_pk</span> <span class="pl-c1">=</span> <span class="pl-s1">sa</span>.<span class="pl-v">Column</span>(<span class="pl-s1">sa</span>.<span class="pl-v">Integer</span>, <span class="pl-s1">sa</span>.<span class="pl-v">ForeignKey</span>(<span class="pl-s">"second.pk"</span>), <span class="pl-s1">nullable</span><span class="pl-c1">=</span><span class="pl-c1">False</span>)


<span class="pl-k">class</span> <span class="pl-v">Second</span>(<span class="pl-v">DeclarativeBase</span>):
    <span class="pl-s1">__tablename__</span> <span class="pl-c1">=</span> <span class="pl-s">"second"</span>

    <span class="pl-s1">pk</span> <span class="pl-c1">=</span> <span class="pl-s1">sa</span>.<span class="pl-v">Column</span>(<span class="pl-s1">sa</span>.<span class="pl-v">Integer</span>, <span class="pl-s1">primary_key</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)


<span class="pl-s1">query</span> <span class="pl-c1">=</span> <span class="pl-s1">session</span>.<span class="pl-en">query</span>(<span class="pl-v">First</span>).<span class="pl-en">filter</span>(<span class="pl-v">First</span>.<span class="pl-s1">pk</span> <span class="pl-c1">!=</span> <span class="pl-c1">1</span>).<span class="pl-en">join</span>(
    <span class="pl-v">Mapper</span>,
    <span class="pl-v">Mapper</span>.<span class="pl-s1">first_pk</span> <span class="pl-c1">==</span> <span class="pl-v">First</span>.<span class="pl-s1">pk</span>,
).<span class="pl-en">join</span>(
    <span class="pl-v">Second</span>,
    <span class="pl-v">Second</span>.<span class="pl-s1">pk</span> <span class="pl-c1">==</span> <span class="pl-v">Mapper</span>.<span class="pl-s1">second_pk</span>,
).<span class="pl-en">with_entities</span>(<span class="pl-v">Second</span>)

<span class="pl-k">if</span> <span class="pl-s1">__name__</span> <span class="pl-c1">==</span> <span class="pl-s">"__main__"</span>:
    <span class="pl-en">print</span>(<span class="pl-s1">query</span>.<span class="pl-s1">statement</span>)</pre></div>
<p dir="auto">You get an output of:</p>
<div class="highlight highlight-source-sql notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="SELECT second.pk 
FROM first, second JOIN mapper ON mapper.first_pk = first.pk JOIN second ON second.pk = mapper.second_pk 
WHERE first.pk != :pk_1"><pre class="notranslate"><span class="pl-k">SELECT</span> <span class="pl-c1">second</span>.<span class="pl-c1">pk</span> 
<span class="pl-k">FROM</span> first, second <span class="pl-k">JOIN</span> mapper <span class="pl-k">ON</span> <span class="pl-c1">mapper</span>.<span class="pl-c1">first_pk</span> <span class="pl-k">=</span> <span class="pl-c1">first</span>.<span class="pl-c1">pk</span> <span class="pl-k">JOIN</span> second <span class="pl-k">ON</span> <span class="pl-c1">second</span>.<span class="pl-c1">pk</span> <span class="pl-k">=</span> <span class="pl-c1">mapper</span>.<span class="pl-c1">second_pk</span> 
<span class="pl-k">WHERE</span> <span class="pl-c1">first</span>.<span class="pl-c1">pk</span> <span class="pl-k">!=</span> :pk_1</pre></div>
<p dir="auto">This is invalid, as the first <code class="notranslate">JOIN</code> references the table in the first <code class="notranslate">FROM</code>. Running this query in Postgresql gives an error message like:</p>
<p dir="auto"><code class="notranslate">invalid reference to FROM-clause entry for table "first" Hint: There is an entry for table "first", but it cannot be referenced from this part of the query.</code></p>
<p dir="auto">In SQLAlchemy 1.3.23 I get this perfectly valid SQL output instead:</p>
<div class="highlight highlight-source-sql notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="SELECT second.pk 
FROM first JOIN mapper ON mapper.first_pk = first.pk JOIN second ON second.pk = mapper.second_pk 
WHERE first.pk != :pk_1"><pre class="notranslate"><span class="pl-k">SELECT</span> <span class="pl-c1">second</span>.<span class="pl-c1">pk</span> 
<span class="pl-k">FROM</span> first <span class="pl-k">JOIN</span> mapper <span class="pl-k">ON</span> <span class="pl-c1">mapper</span>.<span class="pl-c1">first_pk</span> <span class="pl-k">=</span> <span class="pl-c1">first</span>.<span class="pl-c1">pk</span> <span class="pl-k">JOIN</span> second <span class="pl-k">ON</span> <span class="pl-c1">second</span>.<span class="pl-c1">pk</span> <span class="pl-k">=</span> <span class="pl-c1">mapper</span>.<span class="pl-c1">second_pk</span> 
<span class="pl-k">WHERE</span> <span class="pl-c1">first</span>.<span class="pl-c1">pk</span> <span class="pl-k">!=</span> :pk_1</pre></div>
<p dir="auto">In my case I think I can pretty easily work around the issue by rewriting the query but I figured I should report it since a straight upgrade caused a test to fail 😅. Thanks!</p>
<p dir="auto"><strong>Versions.</strong></p>
<ul dir="auto">
<li>OS: macOS 11.3.1</li>
<li>Python: 3.8.9</li>
<li>SQLAlchemy: 1.4.15</li>
<li>Database: Postgres 10.12</li>
<li>DBAPI: psycopg2</li>
</ul>
<p dir="auto"><strong>Describe the bug</strong><br>
Very similar to <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="832235660" data-permission-text="Title is private" data-url="https://github.com/sqlalchemy/sqlalchemy/issues/6052" data-hovercard-type="issue" data-hovercard-url="/sqlalchemy/sqlalchemy/issues/6052/hovercard" href="https://github.com/sqlalchemy/sqlalchemy/issues/6052">#6052</a> (which is supposedly fixed?), applying any loader option breaks <code class="notranslate">with_entities()</code>.</p>
<p dir="auto"><strong>Expected behavior</strong><br>
No error, this output from the test script:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="normal
[&lt;Thing 1&gt;]
with entities + noload
[(1,)]
with entities + joinedload
[(1,)]"><pre class="notranslate"><code class="notranslate">normal
[&lt;Thing 1&gt;]
with entities + noload
[(1,)]
with entities + joinedload
[(1,)]
</code></pre></div>
<p dir="auto"><strong>To Reproduce</strong></p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base  # to test on 1.3
from sqlalchemy.orm import *


Base = declarative_base()


class Thing(Base):
    __tablename__ = 'things'
    id = Column(Integer, primary_key=True)
    owner_id = Column(Integer, ForeignKey('users.id'), nullable=True, index=True)
    owner = relationship('User', backref='things')

    def __repr__(self):
        return f'&lt;Thing {self.id}&gt;'


class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)

    def __repr__(self):
        return f'&lt;User {self.id}&gt;'


e = create_engine('postgresql:///test', echo=False)
Base.metadata.create_all(e)
s = Session(e)

if not s.query(Thing).count():
    thing = Thing(owner=User())
    s.add(thing)
    s.commit()


print('normal')
print(s.query(Thing).all())
print('with entities + noload')  # fails
print(s.query(Thing).options(noload('owner')).with_entities(Thing.id).all())
print('with entities + joinedload')  # fails
print(s.query(Thing).options(joinedload('owner')).with_entities(Thing.id).all())"><pre class="notranslate"><span class="pl-k">from</span> <span class="pl-s1">sqlalchemy</span> <span class="pl-k">import</span> <span class="pl-c1">*</span>
<span class="pl-k">from</span> <span class="pl-s1">sqlalchemy</span>.<span class="pl-s1">ext</span>.<span class="pl-s1">declarative</span> <span class="pl-k">import</span> <span class="pl-s1">declarative_base</span>  <span class="pl-c"># to test on 1.3</span>
<span class="pl-k">from</span> <span class="pl-s1">sqlalchemy</span>.<span class="pl-s1">orm</span> <span class="pl-k">import</span> <span class="pl-c1">*</span>


<span class="pl-v">Base</span> <span class="pl-c1">=</span> <span class="pl-en">declarative_base</span>()


<span class="pl-k">class</span> <span class="pl-v">Thing</span>(<span class="pl-v">Base</span>):
    <span class="pl-s1">__tablename__</span> <span class="pl-c1">=</span> <span class="pl-s">'things'</span>
    <span class="pl-s1">id</span> <span class="pl-c1">=</span> <span class="pl-v">Column</span>(<span class="pl-v">Integer</span>, <span class="pl-s1">primary_key</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)
    <span class="pl-s1">owner_id</span> <span class="pl-c1">=</span> <span class="pl-v">Column</span>(<span class="pl-v">Integer</span>, <span class="pl-v">ForeignKey</span>(<span class="pl-s">'users.id'</span>), <span class="pl-s1">nullable</span><span class="pl-c1">=</span><span class="pl-c1">True</span>, <span class="pl-s1">index</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)
    <span class="pl-s1">owner</span> <span class="pl-c1">=</span> <span class="pl-en">relationship</span>(<span class="pl-s">'User'</span>, <span class="pl-s1">backref</span><span class="pl-c1">=</span><span class="pl-s">'things'</span>)

    <span class="pl-k">def</span> <span class="pl-en">__repr__</span>(<span class="pl-s1">self</span>):
        <span class="pl-k">return</span> <span class="pl-s">f'&lt;Thing <span class="pl-s1"><span class="pl-kos">{</span><span class="pl-s1">self</span>.<span class="pl-s1">id</span><span class="pl-kos">}</span></span>&gt;'</span>


<span class="pl-k">class</span> <span class="pl-v">User</span>(<span class="pl-v">Base</span>):
    <span class="pl-s1">__tablename__</span> <span class="pl-c1">=</span> <span class="pl-s">'users'</span>
    <span class="pl-s1">id</span> <span class="pl-c1">=</span> <span class="pl-v">Column</span>(<span class="pl-v">Integer</span>, <span class="pl-s1">primary_key</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)

    <span class="pl-k">def</span> <span class="pl-en">__repr__</span>(<span class="pl-s1">self</span>):
        <span class="pl-k">return</span> <span class="pl-s">f'&lt;User <span class="pl-s1"><span class="pl-kos">{</span><span class="pl-s1">self</span>.<span class="pl-s1">id</span><span class="pl-kos">}</span></span>&gt;'</span>


<span class="pl-s1">e</span> <span class="pl-c1">=</span> <span class="pl-en">create_engine</span>(<span class="pl-s">'postgresql:///test'</span>, <span class="pl-s1">echo</span><span class="pl-c1">=</span><span class="pl-c1">False</span>)
<span class="pl-v">Base</span>.<span class="pl-s1">metadata</span>.<span class="pl-en">create_all</span>(<span class="pl-s1">e</span>)
<span class="pl-s1">s</span> <span class="pl-c1">=</span> <span class="pl-v">Session</span>(<span class="pl-s1">e</span>)

<span class="pl-k">if</span> <span class="pl-c1">not</span> <span class="pl-s1">s</span>.<span class="pl-en">query</span>(<span class="pl-v">Thing</span>).<span class="pl-en">count</span>():
    <span class="pl-s1">thing</span> <span class="pl-c1">=</span> <span class="pl-v">Thing</span>(<span class="pl-s1">owner</span><span class="pl-c1">=</span><span class="pl-v">User</span>())
    <span class="pl-s1">s</span>.<span class="pl-en">add</span>(<span class="pl-s1">thing</span>)
    <span class="pl-s1">s</span>.<span class="pl-en">commit</span>()


<span class="pl-en">print</span>(<span class="pl-s">'normal'</span>)
<span class="pl-en">print</span>(<span class="pl-s1">s</span>.<span class="pl-en">query</span>(<span class="pl-v">Thing</span>).<span class="pl-en">all</span>())
<span class="pl-en">print</span>(<span class="pl-s">'with entities + noload'</span>)  <span class="pl-c"># fails</span>
<span class="pl-en">print</span>(<span class="pl-s1">s</span>.<span class="pl-en">query</span>(<span class="pl-v">Thing</span>).<span class="pl-en">options</span>(<span class="pl-en">noload</span>(<span class="pl-s">'owner'</span>)).<span class="pl-en">with_entities</span>(<span class="pl-v">Thing</span>.<span class="pl-s1">id</span>).<span class="pl-en">all</span>())
<span class="pl-en">print</span>(<span class="pl-s">'with entities + joinedload'</span>)  <span class="pl-c"># fails</span>
<span class="pl-en">print</span>(<span class="pl-s1">s</span>.<span class="pl-en">query</span>(<span class="pl-v">Thing</span>).<span class="pl-en">options</span>(<span class="pl-en">joinedload</span>(<span class="pl-s">'owner'</span>)).<span class="pl-en">with_entities</span>(<span class="pl-v">Thing</span>.<span class="pl-s1">id</span>).<span class="pl-en">all</span>())</pre></div>
<p dir="auto"><strong>Error</strong></p>
<div class="highlight highlight-text-python-traceback notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="normal
[&lt;Thing 1&gt;]
with entities + noload
Traceback (most recent call last):
  File &quot;/home/adrian/dev/indico/py3/src/satest.py&quot;, line 40, in &lt;module&gt;
    print(s.query(Thing).options(noload('owner')).with_entities(Thing.id).all())
  File &quot;/home/adrian/dev/indico/py3/env/lib/python3.9/site-packages/sqlalchemy/orm/query.py&quot;, line 2685, in all
    return self._iter().all()
  File &quot;/home/adrian/dev/indico/py3/env/lib/python3.9/site-packages/sqlalchemy/orm/query.py&quot;, line 2820, in _iter
    result = self.session.execute(
  File &quot;/home/adrian/dev/indico/py3/env/lib/python3.9/site-packages/sqlalchemy/orm/session.py&quot;, line 1670, in execute
    result = conn._execute_20(statement, params or {}, execution_options)
  File &quot;/home/adrian/dev/indico/py3/env/lib/python3.9/site-packages/sqlalchemy/engine/base.py&quot;, line 1520, in _execute_20
    return meth(self, args_10style, kwargs_10style, execution_options)
  File &quot;/home/adrian/dev/indico/py3/env/lib/python3.9/site-packages/sqlalchemy/sql/elements.py&quot;, line 313, in _execute_on_connection
    return connection._execute_clauseelement(
  File &quot;/home/adrian/dev/indico/py3/env/lib/python3.9/site-packages/sqlalchemy/engine/base.py&quot;, line 1381, in _execute_clauseelement
    compiled_sql, extracted_params, cache_hit = elem._compile_w_cache(
  File &quot;/home/adrian/dev/indico/py3/env/lib/python3.9/site-packages/sqlalchemy/sql/elements.py&quot;, line 535, in _compile_w_cache
    compiled_sql = self._compiler(
  File &quot;/home/adrian/dev/indico/py3/env/lib/python3.9/site-packages/sqlalchemy/sql/elements.py&quot;, line 570, in _compiler
    return dialect.statement_compiler(dialect, self, **kw)
  File &quot;/home/adrian/dev/indico/py3/env/lib/python3.9/site-packages/sqlalchemy/sql/compiler.py&quot;, line 766, in __init__
    Compiled.__init__(self, dialect, statement, **kwargs)
  File &quot;/home/adrian/dev/indico/py3/env/lib/python3.9/site-packages/sqlalchemy/sql/compiler.py&quot;, line 455, in __init__
    self.string = self.process(self.statement, **compile_kwargs)
  File &quot;/home/adrian/dev/indico/py3/env/lib/python3.9/site-packages/sqlalchemy/sql/compiler.py&quot;, line 490, in process
    return obj._compiler_dispatch(self, **kwargs)
  File &quot;/home/adrian/dev/indico/py3/env/lib/python3.9/site-packages/sqlalchemy/sql/visitors.py&quot;, line 81, in _compiler_dispatch
    return meth(self, **kw)
  File &quot;/home/adrian/dev/indico/py3/env/lib/python3.9/site-packages/sqlalchemy/sql/compiler.py&quot;, line 2966, in visit_select
    compile_state = select_stmt._compile_state_factory(
  File &quot;/home/adrian/dev/indico/py3/env/lib/python3.9/site-packages/sqlalchemy/sql/base.py&quot;, line 492, in create_for_statement
    return klass.create_for_statement(statement, compiler, **kw)
  File &quot;/home/adrian/dev/indico/py3/env/lib/python3.9/site-packages/sqlalchemy/orm/context.py&quot;, line 579, in create_for_statement
    opt.process_compile_state(self)
  File &quot;/home/adrian/dev/indico/py3/env/lib/python3.9/site-packages/sqlalchemy/orm/strategy_options.py&quot;, line 182, in process_compile_state
    self._process(compile_state, not bool(compile_state.current_path))
  File &quot;/home/adrian/dev/indico/py3/env/lib/python3.9/site-packages/sqlalchemy/orm/strategy_options.py&quot;, line 705, in _process
    val._bind_loader(
  File &quot;/home/adrian/dev/indico/py3/env/lib/python3.9/site-packages/sqlalchemy/orm/strategy_options.py&quot;, line 852, in _bind_loader
    entity = self._find_entity_basestring(entities, token, raiseerr)
  File &quot;/home/adrian/dev/indico/py3/env/lib/python3.9/site-packages/sqlalchemy/orm/strategy_options.py&quot;, line 983, in _find_entity_basestring
    raise sa_exc.ArgumentError(
sqlalchemy.exc.ArgumentError: Query has only expression-based entities - can't find property named &quot;owner&quot;."><pre class="notranslate">normal
[&lt;Thing 1&gt;]
with entities + noload
Traceback (most recent call last):
  File <span class="pl-s">"/home/adrian/dev/indico/py3/src/satest.py"</span>, line <span class="pl-c1">40</span>, in <span class="pl-en">&lt;module&gt;</span>
    <span class="pl-c1">print</span>(s.query(Thing).options(noload(<span class="pl-s"><span class="pl-pds">'</span>owner<span class="pl-pds">'</span></span>)).with_entities(Thing.id).all())
  File <span class="pl-s">"/home/adrian/dev/indico/py3/env/lib/python3.9/site-packages/sqlalchemy/orm/query.py"</span>, line <span class="pl-c1">2685</span>, in <span class="pl-en">all</span>
    <span class="pl-k">return</span> <span class="pl-c1">self</span>._iter().all()
  File <span class="pl-s">"/home/adrian/dev/indico/py3/env/lib/python3.9/site-packages/sqlalchemy/orm/query.py"</span>, line <span class="pl-c1">2820</span>, in <span class="pl-en">_iter</span>
    result <span class="pl-k">=</span> <span class="pl-c1">self</span>.session.execute(
  File <span class="pl-s">"/home/adrian/dev/indico/py3/env/lib/python3.9/site-packages/sqlalchemy/orm/session.py"</span>, line <span class="pl-c1">1670</span>, in <span class="pl-en">execute</span>
    result <span class="pl-k">=</span> conn._execute_20(statement, params <span class="pl-k">or</span> {}, execution_options)
  File <span class="pl-s">"/home/adrian/dev/indico/py3/env/lib/python3.9/site-packages/sqlalchemy/engine/base.py"</span>, line <span class="pl-c1">1520</span>, in <span class="pl-en">_execute_20</span>
    <span class="pl-k">return</span> meth(<span class="pl-c1">self</span>, args_10style, kwargs_10style, execution_options)
  File <span class="pl-s">"/home/adrian/dev/indico/py3/env/lib/python3.9/site-packages/sqlalchemy/sql/elements.py"</span>, line <span class="pl-c1">313</span>, in <span class="pl-en">_execute_on_connection</span>
    <span class="pl-k">return</span> connection._execute_clauseelement(
  File <span class="pl-s">"/home/adrian/dev/indico/py3/env/lib/python3.9/site-packages/sqlalchemy/engine/base.py"</span>, line <span class="pl-c1">1381</span>, in <span class="pl-en">_execute_clauseelement</span>
    compiled_sql, extracted_params, cache_hit <span class="pl-k">=</span> elem._compile_w_cache(
  File <span class="pl-s">"/home/adrian/dev/indico/py3/env/lib/python3.9/site-packages/sqlalchemy/sql/elements.py"</span>, line <span class="pl-c1">535</span>, in <span class="pl-en">_compile_w_cache</span>
    compiled_sql <span class="pl-k">=</span> <span class="pl-c1">self</span>._compiler(
  File <span class="pl-s">"/home/adrian/dev/indico/py3/env/lib/python3.9/site-packages/sqlalchemy/sql/elements.py"</span>, line <span class="pl-c1">570</span>, in <span class="pl-en">_compiler</span>
    <span class="pl-k">return</span> dialect.statement_compiler(dialect, <span class="pl-c1">self</span>, <span class="pl-k">**</span>kw)
  File <span class="pl-s">"/home/adrian/dev/indico/py3/env/lib/python3.9/site-packages/sqlalchemy/sql/compiler.py"</span>, line <span class="pl-c1">766</span>, in <span class="pl-en">__init__</span>
    Compiled.<span class="pl-c1">__init__</span>(<span class="pl-c1">self</span>, dialect, statement, <span class="pl-k">**</span>kwargs)
  File <span class="pl-s">"/home/adrian/dev/indico/py3/env/lib/python3.9/site-packages/sqlalchemy/sql/compiler.py"</span>, line <span class="pl-c1">455</span>, in <span class="pl-en">__init__</span>
    <span class="pl-c1">self</span>.string <span class="pl-k">=</span> <span class="pl-c1">self</span>.process(<span class="pl-c1">self</span>.statement, <span class="pl-k">**</span>compile_kwargs)
  File <span class="pl-s">"/home/adrian/dev/indico/py3/env/lib/python3.9/site-packages/sqlalchemy/sql/compiler.py"</span>, line <span class="pl-c1">490</span>, in <span class="pl-en">process</span>
    <span class="pl-k">return</span> obj._compiler_dispatch(<span class="pl-c1">self</span>, <span class="pl-k">**</span>kwargs)
  File <span class="pl-s">"/home/adrian/dev/indico/py3/env/lib/python3.9/site-packages/sqlalchemy/sql/visitors.py"</span>, line <span class="pl-c1">81</span>, in <span class="pl-en">_compiler_dispatch</span>
    <span class="pl-k">return</span> meth(<span class="pl-c1">self</span>, <span class="pl-k">**</span>kw)
  File <span class="pl-s">"/home/adrian/dev/indico/py3/env/lib/python3.9/site-packages/sqlalchemy/sql/compiler.py"</span>, line <span class="pl-c1">2966</span>, in <span class="pl-en">visit_select</span>
    compile_state <span class="pl-k">=</span> select_stmt._compile_state_factory(
  File <span class="pl-s">"/home/adrian/dev/indico/py3/env/lib/python3.9/site-packages/sqlalchemy/sql/base.py"</span>, line <span class="pl-c1">492</span>, in <span class="pl-en">create_for_statement</span>
    <span class="pl-k">return</span> klass.create_for_statement(statement, compiler, <span class="pl-k">**</span>kw)
  File <span class="pl-s">"/home/adrian/dev/indico/py3/env/lib/python3.9/site-packages/sqlalchemy/orm/context.py"</span>, line <span class="pl-c1">579</span>, in <span class="pl-en">create_for_statement</span>
    opt.process_compile_state(<span class="pl-c1">self</span>)
  File <span class="pl-s">"/home/adrian/dev/indico/py3/env/lib/python3.9/site-packages/sqlalchemy/orm/strategy_options.py"</span>, line <span class="pl-c1">182</span>, in <span class="pl-en">process_compile_state</span>
    <span class="pl-c1">self</span>._process(compile_state, <span class="pl-k">not</span> <span class="pl-c1">bool</span>(compile_state.current_path))
  File <span class="pl-s">"/home/adrian/dev/indico/py3/env/lib/python3.9/site-packages/sqlalchemy/orm/strategy_options.py"</span>, line <span class="pl-c1">705</span>, in <span class="pl-en">_process</span>
    val._bind_loader(
  File <span class="pl-s">"/home/adrian/dev/indico/py3/env/lib/python3.9/site-packages/sqlalchemy/orm/strategy_options.py"</span>, line <span class="pl-c1">852</span>, in <span class="pl-en">_bind_loader</span>
    entity <span class="pl-k">=</span> <span class="pl-c1">self</span>._find_entity_basestring(entities, token, raiseerr)
  File <span class="pl-s">"/home/adrian/dev/indico/py3/env/lib/python3.9/site-packages/sqlalchemy/orm/strategy_options.py"</span>, line <span class="pl-c1">983</span>, in <span class="pl-en">_find_entity_basestring</span>
    <span class="pl-k">raise</span> sa_exc.ArgumentError(
<span class="pl-en">sqlalchemy.exc.ArgumentError</span>: <span class="pl-s">Query has only expression-based entities - can't find property named "owner".</span></pre></div>
<p dir="auto"><strong>Additional context</strong></p>
<p dir="auto">The example script is minimal, hence the "strange" usage of <code class="notranslate">with_entities</code>. In my real application the query is coming from a util function which applies a joinedload option, and overriding it with a noload doesn't help because <em>any</em> load option triggers this bug.</p>
<p dir="auto"><strong>Versions.</strong></p>
<ul dir="auto">
<li>OS: Linux</li>
<li>Python: 3.9.1</li>
<li>SQLAlchemy: 1.4.7</li>
<li>Database: Postgres</li>
<li>DBAPI: psycopg2</li>
</ul>