peewee DoubleField的默认值不会反映在MySQL db中(Default value for peewee DoubleField doesn't get reflected

编程入门 行业动态 更新时间:2024-10-24 16:25:13
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

更多推荐

本文发布于:2023-07-09 02:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1082884.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:默认值   DoubleField   peewee   MySQL   reflected

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!