【发布时间】:2020-12-24 18:51:19
【问题描述】:
我在为 nestjs-typeorm-mongo 项目创建初始迁移时遇到问题。
我已经克隆了this sample project from nestjs that uses typeorm with mongodb。该项目确实有效,当我将“照片”文档放入本地 mongo 并使用名为“test”的数据库并收集“照片”之后在本地运行它时,我可以调用 localhost:3000/photo 并接收照片文档。
现在我正在尝试使用以下命令使用 typeorm cli 创建迁移:
./node_modules/.bin/ts-node ./node_modules/typeorm/cli.js migration:generate -n initial
...但它不起作用。我无法创建初始提交 - 即使在我的 app.module.ts 文件中设置“同步:false”后,我总是会收到错误:
未发现数据库架构更改 - 无法生成迁移。要创建新的空迁移,请使用“typeorm migration:create”命令 尝试生成迁移时... ????
除了将 synchronize 更改为 false 之外,我所做的唯一其他更改是通过运行 typeorm init --database mongodb 在项目根目录中添加 ormconfig.json 文件:
{
"type": "mongodb",
"database": "test",
"synchronize": true,
"logging": false,
"entities": [
"src/**/*.entity.ts"
],
"migrations": [
"src/migration/**/*.ts"
],
"subscribers": [
"src/subscriber/**/*.ts"
],
"cli": {
"entitiesDir": "src",
"migrationsDir": "src/migration",
"subscribersDir": "src/subscriber"
}
}
【问题讨论】:
-
你确定使用 mongodb 和 mongodb 需要迁移吗?我认为它总是在连接到 db 时创建数据库和集合,如果它不存在。
-
@spikie 当我将字段添加到数据模型时,它也不会创建任何迁移...
标签: mongodb database-migration nestjs typeorm