Skip to content

[sqlalchemy] Exception: Instance %s has a NULL identity key #323

Duplicate

Description

@wibbico

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

Activity

  1. changed the title [-]Exception: Instance %s has a NULL identity key[/-] [+][sqlalchemy] Exception: Instance %s has a NULL identity key[/+] on Jan 22, 2024
  2. susodapop commented on Jan 22, 2024

    @susodapop
    Contributor

    Thanks for opening this issue. You've hit one of the limitations of this dialect, which is documented here. Namely, the autoincrement argument is currently ignored. The handling of server_default= isn't captured in the sqlalchemy compliance test suite either so it's effectively untested.

    This feature is a focus for development. Would you please send an email to databricks-sql-connector-maintainers@databricks.com so we can associate your organization with the internal work item?

    The most important thing we need to know is what version of sqlalchemy you have installed.

  3. wibbico commented on Jan 23, 2024

    @wibbico
    Author

    This is bad, I've read all the documentation, but it seemed that this was just a DDL limitation.

    We have an application scenario where we need to be able to insert objects. This could be a showstopper for us.

    Is there any timelime for this feature?

    We are currently using SQLAlchemy 2.0.25.

  4. susodapop commented on Jan 23, 2024

    @susodapop
    Contributor

    I've seen your email and will respond during normal work hours (middle of the night for me at present). But have you looked at the documentation Iinked above? You are able to set autoincrementing columns with this dialect. You just can't use the autoincrement= argument on your columns. Instead, you have to declare the identity for the column explicitly. An example of this is given.

  5. wibbico commented on Jan 23, 2024

    @wibbico
    Author

    This was the documentation i referred to, but unfortunately it does not work

    from sqlalchemy import Identity, String
    
    class SomeModel(Base):
        id      = BigInteger(Identity())
        value   = String()
    

    Raises error

    TypeError: BigInteger() takes no arguments

    How can I fix the example from above?

  6. susodapop commented on Jan 23, 2024

    @susodapop
    Contributor
  7. wibbico commented on Jan 23, 2024

    @wibbico
    Author

    Iam trying to get as close to the docs as possible, still no success:

    @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 dummy2")
                session.execute(rollback_query_kopf)
                session.commit()
    
    
    class Base(DeclarativeBase):
        pass
    
    
    class SomeModel(Base):
        __tablename__ = "dummy2"
    
        # primary_key=True is needed: sqlalchemy.exc.ArgumentError: 
        # Mapper Mapper[SomeModel(...)] could not assemble any primary key columns for mapped table 'dummy2'
        id = Column(BigInteger, Identity(), primary_key=True)
        value = Column(String)
    
    
    def test_insert_row_autogenerate(fix_session):
        sample = SomeModel(value="test")
        fix_session.add(sample)
        fix_session.commit()
    
        actual = fix_session.query(SomeModel).first()
    
        assert actual
    

    Still error

    sqlalchemy.orm.exc.FlushError: Instance <SomeModel at 0x7fd5b9134050> 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.

  8. susodapop commented on Jan 23, 2024

    @susodapop
    Contributor
  9. susodapop commented on Jan 23, 2024

    @susodapop
    Contributor

    Alright I've confirmed what's going on here and pushed a branch with a test file here to show my work.

    tl;dr if you are inserting records with sqlalchemy core, then auto-incrementing works just fine. if you need to use the ORM it will fail with the error you see because databricks-sql-connector has no way to return the id of the last inserted row.

    I'm researching internally to see what is the status on adding the bolded behaviour above.

    Here's an example without ORM that works

    class TestIdentityColumnBehavior(IdentityTestBase):
        @pytest.fixture
        def table(self, engine):
            md = MetaData()
            tbl = Table(
                "foo",
                md,
                Column("bar", String, comment="column comment"),
                Column("id", BigInteger, Identity()),
            )
            md.create_all(bind=engine)
    
            yield tbl
    
            md.drop_all(bind=engine)
    
        def test_identity_column_autoincrements(self, engine: Engine, table: Table):
            stmt1 = table.insert().values(bar="baz")
            stmt2 = table.select()
            with engine.begin() as conn:
                conn.execute(stmt1)
                result = conn.execute(stmt2).fetchone()
    
            assert result.id == 1
  10. susodapop commented on Jan 23, 2024

    @susodapop
    Contributor

    Will need to update our documentation with more specificity about what's supported at the moment. For your usage, are you able to drop into sqlalchemy core rather than using the ORM?

  11. kostahuffman commented on Jun 17, 2025

    @kostahuffman

    Alright I've confirmed what's going on here and pushed a branch with a test file here to show my work.

    tl;dr if you are inserting records with sqlalchemy core, then auto-incrementing works just fine. if you need to use the ORM it will fail with the error you see because databricks-sql-connector has no way to return the id of the last inserted row.

    I'm researching internally to see what is the status on adding the bolded behaviour above.

    Here's an example without ORM that works

    class TestIdentityColumnBehavior(IdentityTestBase):
    @pytest.fixture
    def table(self, engine):
    md = MetaData()
    tbl = Table(
    "foo",
    md,
    Column("bar", String, comment="column comment"),
    Column("id", BigInteger, Identity()),
    )
    md.create_all(bind=engine)

        yield tbl
    
        md.drop_all(bind=engine)
    
    def test_identity_column_autoincrements(self, engine: Engine, table: Table):
        stmt1 = table.insert().values(bar="baz")
        stmt2 = table.select()
        with engine.begin() as conn:
            conn.execute(stmt1)
            result = conn.execute(stmt2).fetchone()
    
        assert result.id == 1
    

    Hi @susodapop, thanks for taking a look at this. By any chance do you know if this issue is still valid / has been addressed in another ticket? or if this change is possible.

  12. konrad-szczepaniak-esky commented on Sep 11, 2025

    @konrad-szczepaniak-esky

    I have the same issue. When I try to insert values:

    from flask_sqlalchemy import SQLAlchemy
    
    db = SQLAlchemy()
    
    
    class Example(db.Model):
           id = db.Column(db.BigInteger, primary_key=True, server_default=db.Identity(), autoincrement=True)
           name = db.Column(db.String)
    
    example = Example(name="name")
    db.session.add(example)
    db.session.commit() #app run error here
    
    

    Is any workaround except Example.__table__.insert().values(...)?

  13. cathleeny commented on Dec 3, 2026

    @cathleeny
    Collaborator

    The SQLAlchemy dialect now lives in databricks/databricks-sqlalchemy, so this issue has been opened there: databricks/databricks-sqlalchemy#87. Please follow it there. Closing here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions