SQLAlchemy 变量驼峰命名自动转换为数据库蛇形命名

问题

在使用flask_sqlalchemy 时,希望能够像Java中的ORM一样,自动把驼峰命名的变量转换为数据库使用下划线的蛇形命名。很遗憾,找了官方文档,flask_sqlalchemy只支持表明自动转换,并不支持变量名/字段名自动转换

If a model does not set __tablename__, it will be generated by converting the class name from CamelCase to snake_case. It will not be generated if the model looks like it uses single-table inheritance.

解决方案

在定义字段时手动指定,例如:

memberId = Column(‘member_id’, String(20), default=‘’, comment=‘xx’)

其中,member_id是字段名,memberId 是变量名。

欢迎关注

人工智能技术分析
人工智能 机器学习 深度学习
AI人工智能三要素:数据、算力和算法
计算机视觉(CV)任务介绍:分类、检测、分割、超分、关键点识别、图像生成、度量学习

公众号持续更新原创内容,欢迎订阅。

AI人工智能与大数据
Alt

猜你喜欢

转载自blog.csdn.net/guanxs/article/details/127520700