我有一个关于续集的小问题,我无法解决,这让我很恼火.
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.
更多推荐
序列化时间戳名称
发布评论