SQL Alchemy NULL标识密钥

时间:2012-10-22 15:15:32

标签: python mysql sqlalchemy flask-sqlalchemy

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

1 个答案:

答案 0 :(得分:1)

我通过删除server_default值

解决了这个问题