<p dir="auto">Catching <code class="notranslate">DBApi</code> exception it would be nice to get the model name or at least the table name which caused the problem.</p>
<p dir="auto">e.orig.args is not convenient:</p>
<p dir="auto"><code class="notranslate">('duplicate key value violates unique constraint "unique_shop_item_user"\nDETAIL:  Key (shop_item_id, user_id)=(1, 96) already exists.\n',)</code></p>
<p dir="auto">due to the ambiguity of a message.</p>
<h3 dir="auto">Describe the bug</h3>
<p dir="auto">Incorrect warning <code class="notranslate">SAWarning: Reassigning polymorphic association for identity ... Check for duplicate use of ... as value for polymorphic_identity</code> under some inheritance configurations.</p>
<p dir="auto">In the example below, there is no duplication of polymorphic identity. But a warning is displayed.</p>
<h3 dir="auto">To Reproduce</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import sqlalchemy as sa
from sqlalchemy import orm


metadata = sa.MetaData()
BaseModel = orm.declarative_base(metadata=metadata)


@orm.declarative_mixin
class PolymorphicMixin:
    @orm.declared_attr
    def discriminator(cls) -&gt; sa.Column:
        return sa.Column(sa.String(40), nullable=False)

    __mapper_args__ = {
        'polymorphic_on': discriminator,
    }


class Entity(BaseModel, PolymorphicMixin):
    __tablename__ = 'entities'
    pk = sa.Column(sa.Integer, primary_key=True)


class Actor(Entity):
    __tablename__ = 'actors'
    __mapper_args__ = {'polymorphic_identity': 'actor'}
    pk = sa.Column(sa.ForeignKey(Entity.pk), primary_key=True)


class Client(Actor):
    __tablename__ = 'clients'
    pk = sa.Column(sa.ForeignKey(Actor.pk), primary_key=True)


class Person(Client):
    __mapper_args__ = {'polymorphic_identity': 'person'}


class Company(Client):
    __mapper_args__ = {'polymorphic_identity': 'company'}"><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-k">import</span> <span class="pl-s1">orm</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">BaseModel</span> <span class="pl-c1">=</span> <span class="pl-s1">orm</span>.<span class="pl-en">declarative_base</span>(<span class="pl-s1">metadata</span><span class="pl-c1">=</span><span class="pl-s1">metadata</span>)


<span class="pl-en">@<span class="pl-s1">orm</span>.<span class="pl-s1">declarative_mixin</span></span>
<span class="pl-k">class</span> <span class="pl-v">PolymorphicMixin</span>:
    <span class="pl-en">@<span class="pl-s1">orm</span>.<span class="pl-s1">declared_attr</span></span>
    <span class="pl-k">def</span> <span class="pl-en">discriminator</span>(<span class="pl-s1">cls</span>) <span class="pl-c1">-&gt;</span> <span class="pl-s1">sa</span>.<span class="pl-v">Column</span>:
        <span class="pl-k">return</span> <span class="pl-s1">sa</span>.<span class="pl-v">Column</span>(<span class="pl-s1">sa</span>.<span class="pl-v">String</span>(<span class="pl-c1">40</span>), <span class="pl-s1">nullable</span><span class="pl-c1">=</span><span class="pl-c1">False</span>)

    <span class="pl-s1">__mapper_args__</span> <span class="pl-c1">=</span> {
        <span class="pl-s">'polymorphic_on'</span>: <span class="pl-s1">discriminator</span>,
    }


<span class="pl-k">class</span> <span class="pl-v">Entity</span>(<span class="pl-v">BaseModel</span>, <span class="pl-v">PolymorphicMixin</span>):
    <span class="pl-s1">__tablename__</span> <span class="pl-c1">=</span> <span class="pl-s">'entities'</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">Actor</span>(<span class="pl-v">Entity</span>):
    <span class="pl-s1">__tablename__</span> <span class="pl-c1">=</span> <span class="pl-s">'actors'</span>
    <span class="pl-s1">__mapper_args__</span> <span class="pl-c1">=</span> {<span class="pl-s">'polymorphic_identity'</span>: <span class="pl-s">'actor'</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">ForeignKey</span>(<span class="pl-v">Entity</span>.<span class="pl-s1">pk</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">Client</span>(<span class="pl-v">Actor</span>):
    <span class="pl-s1">__tablename__</span> <span class="pl-c1">=</span> <span class="pl-s">'clients'</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">ForeignKey</span>(<span class="pl-v">Actor</span>.<span class="pl-s1">pk</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">Person</span>(<span class="pl-v">Client</span>):
    <span class="pl-s1">__mapper_args__</span> <span class="pl-c1">=</span> {<span class="pl-s">'polymorphic_identity'</span>: <span class="pl-s">'person'</span>}


<span class="pl-k">class</span> <span class="pl-v">Company</span>(<span class="pl-v">Client</span>):
    <span class="pl-s1">__mapper_args__</span> <span class="pl-c1">=</span> {<span class="pl-s">'polymorphic_identity'</span>: <span class="pl-s">'company'</span>}</pre></div>
<h3 dir="auto">Error</h3>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="test_example.py:31: SAWarning: Reassigning polymorphic association for identity 'actor' from &lt;Mapper at 0x7ff89b8d6070; Actor&gt; to &lt;Mapper at 0x7ff89b8d61c0; User&gt;: Check for duplicate use of 'actor' as value for polymorphic_identity.
  class User(Actor):"><pre class="notranslate"><code class="notranslate">test_example.py:31: SAWarning: Reassigning polymorphic association for identity 'actor' from &lt;Mapper at 0x7ff89b8d6070; Actor&gt; to &lt;Mapper at 0x7ff89b8d61c0; User&gt;: Check for duplicate use of 'actor' as value for polymorphic_identity.
  class User(Actor):
</code></pre></div>
<h3 dir="auto">Versions</h3>
<ul dir="auto">
<li>OS: Linux Mint 20.2</li>
<li>Python: 3.9.5</li>
<li>SQLAlchemy: 1.4.22</li>
</ul>
<h3 dir="auto">Additional context</h3>
<p dir="auto">If the <code class="notranslate">polymorphic_identity</code> argument is removed from the <code class="notranslate">Actor</code> class, there will be no warning.</p>
<p dir="auto">If the <code class="notranslate">polymorphic_identity</code> argument is added to the <code class="notranslate">Client</code> class, there will be no warning.</p>