[sqlalchemy] Exception: Instance %s has a NULL identity key
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
- 233
- Forks
- 152
- Avg merge
- 21h 5m
- Merged PRs (30d)
- 10
Contributor 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 78/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
All issues in databricks/databricks-sql-python
Similar issues
-
fix: inaccuracy ⚠️
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
uabrc/uabrc.github.io#1255 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
ethereum-optimism/factory#64 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
duckdb/duckdb-python#627 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
documentation
Difficulty 1/5 Under an hour Newbie friendliness 78/100
Qiskit/qiskit-addon-sqd#376 ·