[sqlalchemy] Exception: Instance %s has a NULL identity key
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
Research direction
Start by running the supplied test_insert_row_autogenerate with the fix_session and databricks_engine fixture, then trace the insert and flush behavior that produces the NULL identity key. Done means the sample row inserts successfully and the autogenerated bigint identity is available when the row is queried.
Written by the indexing model from the issue text.
Description
I cant get autogenerated identity columns to work.
The table is created just fine, but sqlalchelmy cant insert any rows
Error:
sqlalchemy.orm.exc.FlushError: Instance <SampleObject at 0x7fc694ddbd90> 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() is not occurring at an inappropriate time, such as within a load() event.
Sample code:
from sqlalchemy import create_engine, text, Column, BigInteger, String, Identity
from sqlalchemy.orm import Session, DeclarativeBase
from sample import databricks_engine
@pytest.fixture()
def fix_session():
engine = databricks_engine()
try:
with Session(engine) as session:
Base.metadata.create_all(engine)
yield session
finally:
with Session(engine) as session:
rollback_query_kopf = text("DROP TABLE dummy")
session.execute(rollback_query_kopf)
session.commit()
class Base(DeclarativeBase):
pass
class SampleObject(Base):
__tablename__ = "dummy"
bigint_col = Column(
BigInteger, primary_key=True, server_default=Identity(), autoincrement="auto"
)
str_col = Column(String)
def test_insert_row_autogenerate(fix_session):
sample = SampleObject(str_col="test")
fix_session.add(sample)
fix_session.commit()
actual = fix_session.query(SampleObject).first()
assert actual
- Dominant language
- Python
- Stars
- 236
- Forks
- 162
- Avg merge
- 13h 13m
- Merged PRs (30d)
- 23
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from databricks/databricks-sql-python
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
databricks/databricks-sql-python#1034 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
databricks/databricks-sql-python#1012 ·
Maintainers usually reply within 1 day
-
Token federation: `_exchange_token` raises KeyError('access_token') on error responses, discarding the real failure reasonPossibly taken @TangoEnSkai claimed this 81 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
databricks/databricks-sql-python#904 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
databricks/databricks-sql-python#865 ·
Maintainers usually reply within 1 day
-
engineer-bot
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
databricks/databricks-sql-python#860 · 3 comments ·
Maintainers usually reply within 1 day
All issues in databricks/databricks-sql-python
Similar issues
-
fuzz fuzz-unconfirmed
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day
-
Switch to ruffOpen
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Qiskit/qiskit-aer#2533 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
etesync/etesync-dav#376 ·