<p dir="auto"><strong>Migrated issue, originally created by mrudula chougule</strong></p>
<p dir="auto">Hi,</p>
<p dir="auto">I am having python+gtk application. I'm currently porting my python application from Ubuntu 10.04 to 14.04. I have sqlalchemy version of 0.8.4-1Ubuntu2.1 on Ubuntu 14.04.<br>
When trying to run my application, when it is trying to insert column into database it is throwing this error:<br>
<strong>sqlalchemy.exc.compileError: Unconsumed column Name</strong></p>
<p dir="auto">Please share your views or ideas to resolve this error.<br>
I'm stuck!!</p>
<p dir="auto">Thanks,<br>
Mrudula</p>
<p dir="auto"><strong>Migrated issue, originally created by Ashley Chaloner</strong></p>
<p dir="auto">This bug appears when running in pypy but not cpython. It appears when using sqlite or postgres backends (these are the only two I've tested with). The bug does not cause a stack trace, but results in duplicate database rows being returned in non-identical sqlalchemy objects.</p>
<h2 dir="auto">Versions</h2>
<ul dir="auto">
<li>pypy: Python 2.7.13 (5.8.0+dfsg-2<del>ppa2</del>ubuntu16.04, Jun 17 2017, 18:50:19) [PyPy 5.8.0 with GCC 5.4.0 20160609]</li>
<li>cffi==1.10.1</li>
<li>greenlet==0.4.12</li>
<li>readline==6.2.4.1</li>
<li>six==1.10.0</li>
<li>SQLAlchemy==1.1.13</li>
<li>(if using <code class="notranslate">UUIDType</code>: SQLAlchemy-Utils==0.32.16)</li>
</ul>
<h2 dir="auto">bash script for venv setup</h2>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="virtualenv -p /usr/bin/pypy ~/test-venv
source ~/test-venv/bin/activate
pip install \
    cffi==1.10.1 \
    greenlet==0.4.12 \
    readline==6.2.4.1 \
    six==1.10.0 \
    SQLAlchemy==1.1.13

# if using UUIDType:
pip install SQLAlchemy-Utils==0.32.16"><pre class="notranslate">virtualenv -p /usr/bin/pypy <span class="pl-k">~</span>/test-venv
<span class="pl-c1">source</span> <span class="pl-k">~</span>/test-venv/bin/activate
pip install \
    cffi==1.10.1 \
    greenlet==0.4.12 \
    readline==6.2.4.1 \
    six==1.10.0 \
    SQLAlchemy==1.1.13

<span class="pl-c"><span class="pl-c">#</span> if using UUIDType:</span>
pip install SQLAlchemy-Utils==0.32.16</pre></div>
<h2 dir="auto">Python script to reproduce bug</h2>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="from sqlalchemy import (
    Column,
    ForeignKey,
    Index,
    Table,
    create_engine
)
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import joinedload, relationship, sessionmaker
from sqlalchemy.types import Integer

Base = declarative_base()


class Base2(Base):

    __abstract__ = True
    attrlist = [&quot;id&quot;]

    # for easier debugging
    def __repr__(self):
        attrs = [&quot;%s:%s&quot; % (a, getattr(self, a)) for a in self.attrlist]
        return &quot;&lt;%s.%s object at 0x%016x: %s&gt;&quot; % (
            self.__class__.__module__, self.__class__.__name__,
            id(self), &quot;, &quot;.join(attrs))


class Scene(Base2):

    __tablename__ = &quot;scene&quot;

    id = Column(Integer, primary_key=True)


class ActScenes(Base2):

    __tablename__ = &quot;act_scenes&quot;
    attrlist = [&quot;id&quot;, &quot;act_id&quot;, &quot;scene_id&quot;]

    id = Column(Integer, primary_key=True)

    act_id = Column(
        Integer(),
        ForeignKey(&quot;act.id&quot;),
        nullable=False)

    scene_id = Column(
        Integer(),
        ForeignKey(&quot;scene.id&quot;),
        nullable=False)
    scene = relationship(Scene, lazy=&quot;joined&quot;)


class Act(Base2):

    __tablename__ = &quot;act&quot;

    id = Column(Integer, primary_key=True)

    scenes = relationship(ActScenes)


acts = Table(
    &quot;acts&quot;, Base.metadata,
    Column(&quot;play_id&quot;, Integer(), ForeignKey(&quot;play.id&quot;)),
    Column(&quot;act_id&quot;, Integer(), ForeignKey(&quot;act.id&quot;))
)
Index(&quot;ix_acts&quot;, acts.c.play_id, acts.c.act_id, unique=True)


class Play(Base2):

    __tablename__ = &quot;play&quot;

    id = Column(Integer, primary_key=True)

    acts = relationship(&quot;Act&quot;, secondary=acts)


def run_test(i):
    &quot;&quot;&quot;Run one test, and return True if something failed.&quot;&quot;&quot;

    engine = create_engine('sqlite:///:memory:')
    asessionmaker = sessionmaker()
    asessionmaker.configure(bind=engine)
    Base.metadata.create_all(engine)
    session = asessionmaker()

    play1 = Play()
    act = Act()
    act.scenes = [
        ActScenes(
            act_id=act.id,
            scene=Scene()
        )
        for _ in xrange(1000)
    ]
    play1.acts.append(act)

    scenecounts1 = len(play1.acts[0].scenes)

    session.add(play1)
    session.commit()

    # Comment out this block and watch the bug vanish.
    session.query(Play).options(
        joinedload(Play.acts).joinedload(Act.scenes)
    ).filter_by(id=play1.id).first()
    # End block.

    scenecounts1_again = len(play1.acts[0].scenes)
    if scenecounts1 != scenecounts1_again:
        print &quot;Iteration {} failed&quot;.format(i)

        seen_scene_ids = set()
        for scene in act.scenes:
            if scene.id in seen_scene_ids:
                # import pdb; pdb.set_trace()
                print &quot;Duplicate scene.id spotted: {}&quot;.format(scene.id)
                scenes_with_id = [s for s in act.scenes if s.id == scene.id]
                print &quot;Scenes with this id:\n{}&quot;.format(
                    &quot;\n&quot;.join(repr(s) for s in scenes_with_id))
            else:
                seen_scene_ids.add(scene.id)

    return scenecounts1 != scenecounts1_again


if __name__ == &quot;__main__&quot;:
    N = 25
    results = [run_test(i) for i in xrange(N)]
    print &quot;{} out of {} failed.&quot;.format(sum(results), N)"><pre class="notranslate"><span class="pl-k">from</span> <span class="pl-s1">sqlalchemy</span> <span class="pl-k">import</span> (
    <span class="pl-v">Column</span>,
    <span class="pl-v">ForeignKey</span>,
    <span class="pl-v">Index</span>,
    <span class="pl-v">Table</span>,
    <span class="pl-s1">create_engine</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">joinedload</span>, <span class="pl-s1">relationship</span>, <span class="pl-s1">sessionmaker</span>
<span class="pl-k">from</span> <span class="pl-s1">sqlalchemy</span>.<span class="pl-s1">types</span> <span class="pl-k">import</span> <span class="pl-v">Integer</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">Base2</span>(<span class="pl-v">Base</span>):

    <span class="pl-s1">__abstract__</span> <span class="pl-c1">=</span> <span class="pl-c1">True</span>
    <span class="pl-s1">attrlist</span> <span class="pl-c1">=</span> [<span class="pl-s">"id"</span>]

    <span class="pl-c"># for easier debugging</span>
    <span class="pl-k">def</span> <span class="pl-en">__repr__</span>(<span class="pl-s1">self</span>):
        <span class="pl-s1">attrs</span> <span class="pl-c1">=</span> [<span class="pl-s">"%s:%s"</span> <span class="pl-c1">%</span> (<span class="pl-s1">a</span>, <span class="pl-en">getattr</span>(<span class="pl-s1">self</span>, <span class="pl-s1">a</span>)) <span class="pl-k">for</span> <span class="pl-s1">a</span> <span class="pl-c1">in</span> <span class="pl-s1">self</span>.<span class="pl-s1">attrlist</span>]
        <span class="pl-k">return</span> <span class="pl-s">"&lt;%s.%s object at 0x%016x: %s&gt;"</span> <span class="pl-c1">%</span> (
            <span class="pl-s1">self</span>.<span class="pl-s1">__class__</span>.<span class="pl-s1">__module__</span>, <span class="pl-s1">self</span>.<span class="pl-s1">__class__</span>.<span class="pl-s1">__name__</span>,
            <span class="pl-en">id</span>(<span class="pl-s1">self</span>), <span class="pl-s">", "</span>.<span class="pl-en">join</span>(<span class="pl-s1">attrs</span>))


<span class="pl-k">class</span> <span class="pl-v">Scene</span>(<span class="pl-v">Base2</span>):

    <span class="pl-s1">__tablename__</span> <span class="pl-c1">=</span> <span class="pl-s">"scene"</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">class</span> <span class="pl-v">ActScenes</span>(<span class="pl-v">Base2</span>):

    <span class="pl-s1">__tablename__</span> <span class="pl-c1">=</span> <span class="pl-s">"act_scenes"</span>
    <span class="pl-s1">attrlist</span> <span class="pl-c1">=</span> [<span class="pl-s">"id"</span>, <span class="pl-s">"act_id"</span>, <span class="pl-s">"scene_id"</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">act_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">"act.id"</span>),
        <span class="pl-s1">nullable</span><span class="pl-c1">=</span><span class="pl-c1">False</span>)

    <span class="pl-s1">scene_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">"scene.id"</span>),
        <span class="pl-s1">nullable</span><span class="pl-c1">=</span><span class="pl-c1">False</span>)
    <span class="pl-s1">scene</span> <span class="pl-c1">=</span> <span class="pl-en">relationship</span>(<span class="pl-v">Scene</span>, <span class="pl-s1">lazy</span><span class="pl-c1">=</span><span class="pl-s">"joined"</span>)


<span class="pl-k">class</span> <span class="pl-v">Act</span>(<span class="pl-v">Base2</span>):

    <span class="pl-s1">__tablename__</span> <span class="pl-c1">=</span> <span class="pl-s">"act"</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">scenes</span> <span class="pl-c1">=</span> <span class="pl-en">relationship</span>(<span class="pl-v">ActScenes</span>)


<span class="pl-s1">acts</span> <span class="pl-c1">=</span> <span class="pl-v">Table</span>(
    <span class="pl-s">"acts"</span>, <span class="pl-v">Base</span>.<span class="pl-s1">metadata</span>,
    <span class="pl-v">Column</span>(<span class="pl-s">"play_id"</span>, <span class="pl-v">Integer</span>(), <span class="pl-v">ForeignKey</span>(<span class="pl-s">"play.id"</span>)),
    <span class="pl-v">Column</span>(<span class="pl-s">"act_id"</span>, <span class="pl-v">Integer</span>(), <span class="pl-v">ForeignKey</span>(<span class="pl-s">"act.id"</span>))
)
<span class="pl-v">Index</span>(<span class="pl-s">"ix_acts"</span>, <span class="pl-s1">acts</span>.<span class="pl-s1">c</span>.<span class="pl-s1">play_id</span>, <span class="pl-s1">acts</span>.<span class="pl-s1">c</span>.<span class="pl-s1">act_id</span>, <span class="pl-s1">unique</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)


<span class="pl-k">class</span> <span class="pl-v">Play</span>(<span class="pl-v">Base2</span>):

    <span class="pl-s1">__tablename__</span> <span class="pl-c1">=</span> <span class="pl-s">"play"</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">acts</span> <span class="pl-c1">=</span> <span class="pl-en">relationship</span>(<span class="pl-s">"Act"</span>, <span class="pl-s1">secondary</span><span class="pl-c1">=</span><span class="pl-s1">acts</span>)


<span class="pl-k">def</span> <span class="pl-en">run_test</span>(<span class="pl-s1">i</span>):
    <span class="pl-s">"""Run one test, and return True if something failed."""</span>

    <span class="pl-s1">engine</span> <span class="pl-c1">=</span> <span class="pl-en">create_engine</span>(<span class="pl-s">'sqlite:///:memory:'</span>)
    <span class="pl-s1">asessionmaker</span> <span class="pl-c1">=</span> <span class="pl-en">sessionmaker</span>()
    <span class="pl-s1">asessionmaker</span>.<span class="pl-en">configure</span>(<span class="pl-s1">bind</span><span class="pl-c1">=</span><span class="pl-s1">engine</span>)
    <span class="pl-v">Base</span>.<span class="pl-s1">metadata</span>.<span class="pl-en">create_all</span>(<span class="pl-s1">engine</span>)
    <span class="pl-s1">session</span> <span class="pl-c1">=</span> <span class="pl-en">asessionmaker</span>()

    <span class="pl-s1">play1</span> <span class="pl-c1">=</span> <span class="pl-v">Play</span>()
    <span class="pl-s1">act</span> <span class="pl-c1">=</span> <span class="pl-v">Act</span>()
    <span class="pl-s1">act</span>.<span class="pl-s1">scenes</span> <span class="pl-c1">=</span> [
        <span class="pl-v">ActScenes</span>(
            <span class="pl-s1">act_id</span><span class="pl-c1">=</span><span class="pl-s1">act</span>.<span class="pl-s1">id</span>,
            <span class="pl-s1">scene</span><span class="pl-c1">=</span><span class="pl-v">Scene</span>()
        )
        <span class="pl-k">for</span> <span class="pl-s1">_</span> <span class="pl-c1">in</span> <span class="pl-en">xrange</span>(<span class="pl-c1">1000</span>)
    ]
    <span class="pl-s1">play1</span>.<span class="pl-s1">acts</span>.<span class="pl-en">append</span>(<span class="pl-s1">act</span>)

    <span class="pl-s1">scenecounts1</span> <span class="pl-c1">=</span> <span class="pl-en">len</span>(<span class="pl-s1">play1</span>.<span class="pl-s1">acts</span>[<span class="pl-c1">0</span>].<span class="pl-s1">scenes</span>)

    <span class="pl-s1">session</span>.<span class="pl-en">add</span>(<span class="pl-s1">play1</span>)
    <span class="pl-s1">session</span>.<span class="pl-en">commit</span>()

    <span class="pl-c"># Comment out this block and watch the bug vanish.</span>
    <span class="pl-s1">session</span>.<span class="pl-en">query</span>(<span class="pl-v">Play</span>).<span class="pl-en">options</span>(
        <span class="pl-en">joinedload</span>(<span class="pl-v">Play</span>.<span class="pl-s1">acts</span>).<span class="pl-en">joinedload</span>(<span class="pl-v">Act</span>.<span class="pl-s1">scenes</span>)
    ).<span class="pl-en">filter_by</span>(<span class="pl-s1">id</span><span class="pl-c1">=</span><span class="pl-s1">play1</span>.<span class="pl-s1">id</span>).<span class="pl-en">first</span>()
    <span class="pl-c"># End block.</span>

    <span class="pl-s1">scenecounts1_again</span> <span class="pl-c1">=</span> <span class="pl-en">len</span>(<span class="pl-s1">play1</span>.<span class="pl-s1">acts</span>[<span class="pl-c1">0</span>].<span class="pl-s1">scenes</span>)
    <span class="pl-k">if</span> <span class="pl-s1">scenecounts1</span> <span class="pl-c1">!=</span> <span class="pl-s1">scenecounts1_again</span>:
        <span class="pl-k">print</span> <span class="pl-s">"Iteration {} failed"</span>.<span class="pl-en">format</span>(<span class="pl-s1">i</span>)

        <span class="pl-s1">seen_scene_ids</span> <span class="pl-c1">=</span> <span class="pl-en">set</span>()
        <span class="pl-k">for</span> <span class="pl-s1">scene</span> <span class="pl-c1">in</span> <span class="pl-s1">act</span>.<span class="pl-s1">scenes</span>:
            <span class="pl-k">if</span> <span class="pl-s1">scene</span>.<span class="pl-s1">id</span> <span class="pl-c1">in</span> <span class="pl-s1">seen_scene_ids</span>:
                <span class="pl-c"># import pdb; pdb.set_trace()</span>
                <span class="pl-k">print</span> <span class="pl-s">"Duplicate scene.id spotted: {}"</span>.<span class="pl-en">format</span>(<span class="pl-s1">scene</span>.<span class="pl-s1">id</span>)
                <span class="pl-s1">scenes_with_id</span> <span class="pl-c1">=</span> [<span class="pl-s1">s</span> <span class="pl-k">for</span> <span class="pl-s1">s</span> <span class="pl-c1">in</span> <span class="pl-s1">act</span>.<span class="pl-s1">scenes</span> <span class="pl-k">if</span> <span class="pl-s1">s</span>.<span class="pl-s1">id</span> <span class="pl-c1">==</span> <span class="pl-s1">scene</span>.<span class="pl-s1">id</span>]
                <span class="pl-k">print</span> <span class="pl-s">"Scenes with this id:<span class="pl-cce">\n</span>{}"</span>.<span class="pl-en">format</span>(
                    <span class="pl-s">"<span class="pl-cce">\n</span>"</span>.<span class="pl-en">join</span>(<span class="pl-en">repr</span>(<span class="pl-s1">s</span>) <span class="pl-k">for</span> <span class="pl-s1">s</span> <span class="pl-c1">in</span> <span class="pl-s1">scenes_with_id</span>))
            <span class="pl-k">else</span>:
                <span class="pl-s1">seen_scene_ids</span>.<span class="pl-en">add</span>(<span class="pl-s1">scene</span>.<span class="pl-s1">id</span>)

    <span class="pl-k">return</span> <span class="pl-s1">scenecounts1</span> <span class="pl-c1">!=</span> <span class="pl-s1">scenecounts1_again</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-v">N</span> <span class="pl-c1">=</span> <span class="pl-c1">25</span>
    <span class="pl-s1">results</span> <span class="pl-c1">=</span> [<span class="pl-en">run_test</span>(<span class="pl-s1">i</span>) <span class="pl-k">for</span> <span class="pl-s1">i</span> <span class="pl-c1">in</span> <span class="pl-en">xrange</span>(<span class="pl-v">N</span>)]
    <span class="pl-k">print</span> <span class="pl-s">"{} out of {} failed."</span>.<span class="pl-en">format</span>(<span class="pl-en">sum</span>(<span class="pl-s1">results</span>), <span class="pl-v">N</span>)</pre></div>
<h2 dir="auto">Sample output</h2>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="(test-venv) user@host:~$ pypy test_chained_joinedload.py
Iteration 0 failed
Duplicate scene.id spotted: 185
Scenes with this id:
&lt;__main__.ActScenes object at 0x00007f77de383948: id:185, act_id:1, scene_id:185&gt;
&lt;__main__.ActScenes object at 0x00007f77de9c4790: id:185, act_id:1, scene_id:185&gt;
1 out of 25 failed."><pre class="notranslate"><code class="notranslate">(test-venv) user@host:~$ pypy test_chained_joinedload.py
Iteration 0 failed
Duplicate scene.id spotted: 185
Scenes with this id:
&lt;__main__.ActScenes object at 0x00007f77de383948: id:185, act_id:1, scene_id:185&gt;
&lt;__main__.ActScenes object at 0x00007f77de9c4790: id:185, act_id:1, scene_id:185&gt;
1 out of 25 failed.
</code></pre></div>