我已经像这样定义了表的模型的 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.