<p dir="auto"><strong>Migrated issue, originally created by Anonymous</strong></p>
<p dir="auto">When creating a FK object, the column argument can be passed in as class.column, a string, or class.<strong>table</strong>.c.column. However, if the column the FK is created on is a primary key, using class.column raises an attribute error:</p>
<p dir="auto">AttributeError: Neither 'InstrumentedAttribute' object nor 'Comparator' object has an attribute 'table'</p>
<p dir="auto">The attached file shows a working FK using class.column (third class), and a defective fourth class that attempts to create a FK on a column that is also a PK using class.column. In the example, the PK is a composite of two columns, however this is not the issue, the code was tested and raised the same error without the second column.</p>
<p dir="auto">In the example, changing class.column for one of the other options listed above resolved the problem. This is not a serious defect as the other mechanisms work fine, but if this is a relatively easy fix it is worth doing as it provides more elegant syntax which is already working for the most part.</p>
<hr>
<p dir="auto">Attachments: <a href="../wiki/imported_issue_attachments/1399/test.py">test.py</a></p>
<p dir="auto"><strong>Describe your question</strong><br>
I intend to insert a lot of rows with unique hash values, and want to skip duplicated records. Using "OR IGNORE" with sqlite3 works fine, but when using "ON CONFLICT DO NOTHING" with pgsql, the session.add passed, but failed at flush, showing flush error like below<br>
<code class="notranslate">sqlalchemy.orm.exc.FlushError: Instance &lt;tracklets at 0x195ec22b580&gt; has a NULL identity key.  If this is an auto-generated value, check that the database table allows generation of new primary key values, and that the mapped Column object is configured to expect these generated values.  Ensure also that this flush table allows generation of new primary key values, and that the mapped Column object is configured to expect these generated values.  Ensure also that this flush() is not occurring at an inappropriate time, such as within a load() event.</code><br>
What should I do and is there a universal method for cross platform insertion with ignore errors, such as an argument of add.</p>
<p dir="auto"><strong>Versions</strong></p>
<ul dir="auto">
<li>OS: Windows 10 x64</li>
<li>Python: 3.8.3</li>
<li>SQLAlchemy: 1.3.20</li>
<li>Database: Postgresql 12</li>
<li>DBAPI: psycopg 2.8.6</li>
</ul>
<p dir="auto"><strong>Have a nice day!</strong></p>