<p dir="auto">Hi.</p>
<p dir="auto">I am trying to upsert sales data (salesId (Primary Key), itemId and description) to a table and I am running into some problems, specifically<br>
(1) an attribute error: <code class="notranslate">AttributeError: 'StrSQLCompiler' object has no attribute 'visit_on_duplicate_key_update'</code> and<br>
(2) an unsupported compilation error: <code class="notranslate">sqlalchemy.exc.UnsupportedCompilationError: Compiler &lt;sqlalchemy.sql.compiler.StrSQLCompiler object at 0x110e4eda0&gt; can't render element of type &lt;class 'sqlalchemy.dialects.mysql.dml.OnDuplicateClause'&gt;</code> error.</p>
<p dir="auto">Both the errors above trigger when I try to print the on_duplicate_key_stmt variable. I am wondering if I did something wrong here. Thanks</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import copy
import sqlalchemy as sa
from sqlalchemy.dialects.mysql import insert

_metadata = sa.MetaData()

Sales = sa.Table(
    'sales',
    _metadata,
    sa.Column('salesId', sa.String(32), primary_key=True),
    sa.Column('itemId', sa.String(32)),
    sa.Column('description', sa.String(32)),
)

sales_data = {'salesId': '1', 'itemId': '1', 'description': 'TESTING'}
sales_data_no_id = copy.deepcopy(sales_data)
sales_id = sales_data_no_id.pop('salesId')

insert_stmt = insert(Sales).values(sales_data)
on_duplicate_key_stmt = insert_stmt.on_duplicate_key_update(**sales_data_no_id)

print(on_duplicate_key_stmt)"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">copy</span>
<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">dialects</span>.<span class="pl-s1">mysql</span> <span class="pl-k">import</span> <span class="pl-s1">insert</span>

<span class="pl-s1">_metadata</span> <span class="pl-c1">=</span> <span class="pl-s1">sa</span>.<span class="pl-v">MetaData</span>()

<span class="pl-v">Sales</span> <span class="pl-c1">=</span> <span class="pl-s1">sa</span>.<span class="pl-v">Table</span>(
    <span class="pl-s">'sales'</span>,
    <span class="pl-s1">_metadata</span>,
    <span class="pl-s1">sa</span>.<span class="pl-v">Column</span>(<span class="pl-s">'salesId'</span>, <span class="pl-s1">sa</span>.<span class="pl-v">String</span>(<span class="pl-c1">32</span>), <span class="pl-s1">primary_key</span><span class="pl-c1">=</span><span class="pl-c1">True</span>),
    <span class="pl-s1">sa</span>.<span class="pl-v">Column</span>(<span class="pl-s">'itemId'</span>, <span class="pl-s1">sa</span>.<span class="pl-v">String</span>(<span class="pl-c1">32</span>)),
    <span class="pl-s1">sa</span>.<span class="pl-v">Column</span>(<span class="pl-s">'description'</span>, <span class="pl-s1">sa</span>.<span class="pl-v">String</span>(<span class="pl-c1">32</span>)),
)

<span class="pl-s1">sales_data</span> <span class="pl-c1">=</span> {<span class="pl-s">'salesId'</span>: <span class="pl-s">'1'</span>, <span class="pl-s">'itemId'</span>: <span class="pl-s">'1'</span>, <span class="pl-s">'description'</span>: <span class="pl-s">'TESTING'</span>}
<span class="pl-s1">sales_data_no_id</span> <span class="pl-c1">=</span> <span class="pl-s1">copy</span>.<span class="pl-en">deepcopy</span>(<span class="pl-s1">sales_data</span>)
<span class="pl-s1">sales_id</span> <span class="pl-c1">=</span> <span class="pl-s1">sales_data_no_id</span>.<span class="pl-en">pop</span>(<span class="pl-s">'salesId'</span>)

<span class="pl-s1">insert_stmt</span> <span class="pl-c1">=</span> <span class="pl-en">insert</span>(<span class="pl-v">Sales</span>).<span class="pl-en">values</span>(<span class="pl-s1">sales_data</span>)
<span class="pl-s1">on_duplicate_key_stmt</span> <span class="pl-c1">=</span> <span class="pl-s1">insert_stmt</span>.<span class="pl-en">on_duplicate_key_update</span>(<span class="pl-c1">**</span><span class="pl-s1">sales_data_no_id</span>)

<span class="pl-en">print</span>(<span class="pl-s1">on_duplicate_key_stmt</span>)</pre></div>
<p dir="auto"><strong>Migrated issue, originally created by Dmytro Starosud (<a href="https://github.com/dima-starosud">@dima-starosud</a>)</strong></p>
<p dir="auto">This is using sqlalchemy 1.2.0b3.</p>
<p dir="auto">I've faced weird behavior shown in following code snippet. Assertion in that test fails. But if you comment either <code class="notranslate">#1</code> or <code class="notranslate">#2</code> or both it will pass.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="        Base = declarative_base()

        class Target(Base):
            __tablename__ = 'target'
            id = Column(Integer, primary_key=True)

        descriptors = lambda: inspect(Target).all_orm_descriptors.keys()

        # these two calls make test fail
        configure_mappers()  #1
        descriptors()        #2

        Target.value = hybrid_property(lambda _: 'Hello!')
        configure_mappers()
        assert 'value' in descriptors()"><pre class="notranslate"><code class="notranslate">        Base = declarative_base()

        class Target(Base):
            __tablename__ = 'target'
            id = Column(Integer, primary_key=True)

        descriptors = lambda: inspect(Target).all_orm_descriptors.keys()

        # these two calls make test fail
        configure_mappers()  #1
        descriptors()        #2

        Target.value = hybrid_property(lambda _: 'Hello!')
        configure_mappers()
        assert 'value' in descriptors()
</code></pre></div>
<p dir="auto">Please let me know if I get it wrong.</p>
<p dir="auto">Thanks a lot in advance!</p>