Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to insert using ORM: Instance <...> has a NULL identity key #1152

Open
rdong8 opened this issue Dec 30, 2024 · 1 comment
Open

Unable to insert using ORM: Instance <...> has a NULL identity key #1152

rdong8 opened this issue Dec 30, 2024 · 1 comment
Assignees
Labels
api: bigquery Issues related to the googleapis/python-bigquery-sqlalchemy API.

Comments

@rdong8
Copy link

rdong8 commented Dec 30, 2024

Example:

class Base(DeclarativeBase):
    pass

class User(Base):
    __tablename__ = "users"
    __table_args__ = {"schema": "app"}

    # Not a real column - just here to satisfy SQLAlchemy's requirement for a primary key for every table
    id: Mapped[int] = orm.mapped_column(
        BigInteger, primary_key=True, autoincrement=True
    )

    email: Mapped[Optional[str]] = orm.mapped_column(String)

The following code fails:

engine = create_engine("bigquery://", echo=True)
Base.metadata.create_all(engine)

row = User(email="[email protected]")

with Session(engine) as session:
    with session.begin():
        session.add(row)
FlushError: Instance <User at 0xffff90209d30> 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.
@product-auto-label product-auto-label bot added the api: bigquery Issues related to the googleapis/python-bigquery-sqlalchemy API. label Dec 30, 2024
@shollyman shollyman assigned chalmerlowe and Linchin and unassigned shollyman Jan 2, 2025
@tswast
Copy link
Collaborator

tswast commented Jan 21, 2025

FWIW: BigQuery does support primary key columns now (not enforced, but used for optimization), but I don't think anything like auto-increment has been implemented in the backend. https://cloud.google.com/blog/products/data-analytics/join-optimizations-with-bigquery-primary-and-foreign-keys

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: bigquery Issues related to the googleapis/python-bigquery-sqlalchemy API.
Projects
None yet
Development

No branches or pull requests

5 participants