序列化时间戳名称

编程入门 行业动态 更新时间:2024-10-28 20:26:27
本文介绍了序列化时间戳名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个关于续集的小问题,我无法解决,这让我很恼火.

I have a small issue with sequelize that I can't get around and it irritates the hell out of me.

我的所有模型属性都使用驼峰命名法,但我使用蛇形大小写将它们保存到数据库 (postgres) 中.这部分工作正常,除非我想使用 sequelize timestamps 选项.

I'm using camelCase for all my model attributes but I'm using snake case to persist them into the database (postgres). That part works fine except if I want to use sequelize timestamps option.

如果我设置 underscored: false,时间戳会以驼峰命名法保存到数据库中.

If I set underscored: false, timestamps get persisted to the database in camelCase.

如果我设置了underscored: true,它们会被持久化到snake_case 中,但随后它们会在模型上被保存在snake_case 中.

If I set underscored: true, they get persisted in snake_case but then they are in snake_case on the model.

我想要实现的是数据库中的snake_case和模型时间戳的camelCase.

感觉这两个选项是相互排斥的.

It feels like those 2 options are mutually exclusive.

谢谢

推荐答案

是的,你可以.关键是结合模型设置和字段映射.我就是做这个的:定义模型时,添加以下字段映射:

Yes, you can. Key is to combine model setting and field mappings. That's what I do: When you defining your model, add following field mappings:

createdAt: { type: Sequelize.DATE, field: 'created_at' }, updatedAt: { type: Sequelize.DATE, field: 'updated_at' }, deletedAt: { type: Sequelize.DATE, field: 'deleted_at' }

然后在模型选项中:

timestamps: true, underscored: true

对我来说就像一种魅力.这样你也可以正确使用 paranoid 选项.

Works like a charm for me. In this way you can also use paranoid option correctly.

更多推荐

序列化时间戳名称

本文发布于:2023-10-22 23:28:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519044.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:名称   时间   序列化

发布评论

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

>www.elefans.com

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