googleapis / googleapis/google-cloud-python

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

Đang mở
#15,769 2 bình luận 0 reaction 1 người được giao Được giao cho @chalmerlowe Xem trên GitHub
api: bigquery priority: p3 type: bug
Ngôn ngữ chính
Python
Star
5.4k
Fork
1.8k
Merge trung bình
3 ngày 4 giờ
Pull request đã merge (30 ngày)
122

Mô tả

Example:

```python
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:

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

row = User(email="my@email.com")

with Session(engine) as session:
with session.begin():
session.add(row)
```

```
FlushError: Instance 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.
```

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.