当使用Nest.js使用TypeORM启动项目时,我使用存档ormconfig.json和下面的命令。
但是在终端中使用命令npm运行迁移:创建-n测试项目将迁移存档保存在根文件夹中,而不保存在ormconfig.json中的配置文件夹后面
错误在哪里?如何使迁移存档转到typeorm/迁移文件夹?
"type": "mysql",
"host": "localhost",
"port": 3306,
"username": "root",
"password": "root",
"database": "ferrari",
"synchronize": true,
"logging": false,
"entities": [
"typeorm/entity/**/*.ts"
],
"migrations": [
"typeorm/migration/**/*.ts"
],
"subscribers": [
"typeorm/subscriber/**/*.ts"
],
"cli": {
"entitiesDir":"typeorm/entity",
"migrationsDir": "typeorm/migration",
"subscribersDir":"typeorm/subscriber"
}
}
这是结构目录
-src
-typeorm
-migrations
ormconfig.json
package.json
发布于 2022-06-03 12:20:18
在搜索了这个问题之后,我发现了下面的解决方案:
我改变了每一个omrconfig.json
的omrconfig.ts
和这个模型
export default {
type: 'mysql',
host: 'localhost',
port: 3306,
username: 'root',
password: 'root',
database: 'test',
synchronize: false,
logging: true,
"entities": [
"src/typeorm/**/*.ts"
],
"migrations": [
"typeorm/migrations/**/*.ts"
],
"subscribers": [
"src/subscriber/**/*.ts"
],
"cli": {
"entitiesDir": "src/typeorm",
"migrationsDir": "typeorm/migrations",
"subscribersDir": "src/typeorm"
}
};
然后,我安装了typeorm CLI全局
npm i -g typeorm
并在package.json
中添加两个脚本,第一个脚本是:
"typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js --config server/environments/migrations.ts"
第二个问题是:
"migrate:create": "typeorm migration:create \"./typeorm/migrations/"
因此,我设法使用终端命令将迁移保存在所选文件夹中的typeorm/迁移中。
npm run migrate:create -n {migrationName}
这个typeORM版本是0.3.6
https://stackoverflow.com/questions/72483698
复制相似问题