繁体   English   中英

SQL Alchemy NULL身份密钥

[英]SQL Alchemy NULL identity key

我已经定义了表格的模型ID字段,如下所示:

id = Column(Integer(15, unsigned=True),
               nullable=False,
               server_default='0',
               primary_key=True,
               unique=True,
               autoincrement=True)

并相应地更改了数据库(MySQL)表,但是仍然在我创建模型并尝试提交时(使用SQLalchemy 0.7.8的Im)

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.

我通过删除server_default值解决了它

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM