peewee DoubleField 的默认值未反映在 MySQL db 中
Default value for peewee DoubleField doesn't get reflected in MySQL db
用例:想要将 0.0
存储为默认值,而没有为 pewee
的 DoubleField
传递任何值。我写了以下代码,但对我不起作用。
class MyRelation(peewee.Model):
id = peewee.PrimaryKeyField()
percentage = peewee.DoubleField(default=0.0)
这是一个api
@api_blueprint.route('/add_data', methods=['POST'])
@http_header
def add_data():
try:
incoming = json.loads(request.data)
data = MyRelation(percentage=incoming["percentage"])
data.save()
return success_response(201,"Data has been inserted :)")
except Exception as e:
log(str(e))
return raise_error(500, str(e))
记录以下错误
10-05-17 05:24:51 Line:199 Message: Error in add_data(),views.api: (1048, "Column 'percentage' cannot be null")
差异仅在 Python 端强制执行——因此,如果您希望服务器强制执行默认值,则需要使用约束:
percentage = peewee.DoubleField(constraints=[SQL('DEFAULT 0.0')])
文档:http://docs.peewee-orm.com/en/latest/peewee/models.html#default-field-values
用例:想要将 0.0
存储为默认值,而没有为 pewee
的 DoubleField
传递任何值。我写了以下代码,但对我不起作用。
class MyRelation(peewee.Model):
id = peewee.PrimaryKeyField()
percentage = peewee.DoubleField(default=0.0)
这是一个api
@api_blueprint.route('/add_data', methods=['POST'])
@http_header
def add_data():
try:
incoming = json.loads(request.data)
data = MyRelation(percentage=incoming["percentage"])
data.save()
return success_response(201,"Data has been inserted :)")
except Exception as e:
log(str(e))
return raise_error(500, str(e))
记录以下错误
10-05-17 05:24:51 Line:199 Message: Error in add_data(),views.api: (1048, "Column 'percentage' cannot be null")
差异仅在 Python 端强制执行——因此,如果您希望服务器强制执行默认值,则需要使用约束:
percentage = peewee.DoubleField(constraints=[SQL('DEFAULT 0.0')])
文档:http://docs.peewee-orm.com/en/latest/peewee/models.html#default-field-values