首页 > 解决方案 > sqlalchemy.orm.exc.FlushError:实例具有 NULL 身份密钥。(甲骨文数据库)

问题描述

我在将数据插入到烧瓶应用程序的表中时遇到问题。提交到数据库时出现以下错误:

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

我了解 Oracle 使用序列而不是 Autoincrement 并在我的 db.Model 中指定了主键:

class TestModel(db.Model):
    __tablename__ = 'TESTTABLE'
    id_seq = Sequence('TEST_SEQ')
    TESTID = db.Column(db.Integer, id_seq, primary_key=True)

我在 Stackoverflow 上看到了这个确切的错误,它通过降级 cx_Oracle 得到了修复,但我没有这样做。序列已在数据库中创建。我正在运行 python3.7、最新版本的 cx_Oracle 和 Oracle 18.1 DB。

标签: pythonoraclesqlalchemyflask-sqlalchemycx-oracle

解决方案


推荐阅读