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")Use case: Want to store 0.0 as default value while no value is passed for a pewee's DoubleField. I wrote following code but didn't work for me.
class MyRelation(peewee.Model): id = peewee.PrimaryKeyField() percentage = peewee.DoubleField(default=0.0)Here is an 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))which logs following error
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
The difference is only enforced on the Python side -- so if you're expecting the server to enforce the default you'll need to use a constraint:
percentage = peewee.DoubleField(constraints=[SQL('DEFAULT 0.0')])Docs: http://docs.peewee-orm.com/en/latest/peewee/models.html#default-field-values
更多推荐
发布评论