【发布时间】:2023-03-28 01:53:01
【问题描述】:
我已经像这样定义了模型的表的 id 字段:
id = Column(Integer(15, unsigned=True),
nullable=False,
server_default='0',
primary_key=True,
unique=True,
autoincrement=True)
并相应地更改了数据库(MySQL)表,但仍然在我创建模型时 并尝试提交它(我使用 SQLalchemy 0.7.8)
m = MyModel(values without defining the id)
session.add(m)
session.commit()
我收到此错误
FlushError: Instance <MyModel at 0x4566990> 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.
【问题讨论】:
-
你能告诉我们你正在使用哪个数据库,你正在使用哪个版本的 sqlalchemy,以及结果对你很重要(即 pks 必须从 0 开始,或者你还好吗?从 1) 开始?
-
我正在使用 SQLalchemy 0.7.8 和 MySQLdb。不,零并不重要。
标签: python mysql sqlalchemy flask-sqlalchemy