【问题标题】:How can I specify column name casing in TypeORM migrations如何在 TypeORM 迁移中指定列名大小写
【发布时间】:2019-03-26 15:58:09
【问题描述】:
我正在使用 typeORM,我想使用迁移而不是同步,因为我在一个团队中工作,实际上要让数据库进入应用程序实际运行的状态是很多乏味的工作。
问题是我在迁移中指定的每个列名都被转换为小写字母,我通过将所有实体道具设为snake_case 来解决这个问题。但是默认情况下(我认为)postgres 会将外键转换为 camelCase,因此我无法在迁移中通过外键将任何内容相互关联。 (因为它需要是 camelCase 但查询被转换为小写)
我有没有说清楚我的问题是什么?
有没有办法解决这个问题,或者有解决方法吗?
【问题讨论】:
标签:
sql
postgresql
typescript
migration
typeorm
【解决方案1】:
通过调整 ormconfig.js 以支持 Typeorm Naming Strategies 包,可以实现允许 TypeOrm 支持从 camelCase 转换为 snake_case 的替代方法。这将允许您在数据库中的代码和数据库命名约定中具有编码约定。
可以通过以下方式设置:
npm i --save typeorm-naming-strategies
然后在您的 ormconfig.js 中,添加以下行:
const SnakeNamingStrategy = require('typeorm-naming-strategies')
.SnakeNamingStrategy;
module.exports = {
name: 'development',
type: 'postgres',
host: 'localhost',
port: 5432,
...
namingStrategy: new SnakeNamingStrategy(),
}
TypeOrm 现在在 postgres 中命名列时将遵循 snake_case 约定