googleapis / googleapis/google-cloud-python

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

未关闭
#15,769 2 条评论 0 个 reaction 已指派 1 人 已指派给 @chalmerlowe 在 GitHub 查看
api: bigquery priority: p3 type: bug
主要语言
Python
星标
5.4k
派生
1.8k
平均合并
3 天 4 小时
30 天内合并 PR
122

描述

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.
```

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。