TypeORM 迁移未发现任何变化

问题描述 投票:0回答:2

我正在尝试使用 TypeOrm 生成迁移。当我更改实体时,它应该检测到此更改并生成新的迁移。

我收到以下错误消息:

未发现数据库架构发生更改 - 无法生成迁移。要创建新的空迁移,请使用“typeorm migration:create”命令

当我更改实体文件中的某些内容时,为什么会收到此错误消息?

我正在使用此命令来运行 TypeOrm:

    "typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js --config src/typeormConfig.ts",

这是我的 typeormConfig.ts 文件:

import { ConnectionOptions } from "typeorm";
import * as path from "path";
import * as dotEnv from 'dotenv'

console.log(path.resolve(__dirname, 'entity/**.entity.ts'))

const result = dotEnv.config({ path: './ormconfig.env' });

if (result.error) {
  throw result.error
}

const config: ConnectionOptions = {
  type: 'mysql',
  host: process.env.TYPEORM_HOST,
  port: +process.env.TYPEORM_PORT,
  username: process.env.TYPEORM_USERNAME,
  password: process.env.TYPEORM_PASSWORD,
  database: process.env.TYPEORM_DATABASE,
  //synchronize: true,
  synchronize: false,
  // migrationsRun: false,
  entities: [path.resolve(__dirname, 'entity/**.entity.*')],
  migrations: [path.resolve(__dirname, 'migration/**')],
  cli: {
    entitiesDir: "src/entity",
    migrationsDir: "src/migration"
  }
}

export default config;
node.js migration typeorm
2个回答
0
投票

像这样更新实体和迁移目录链接

entities: [__dirname + '/entity/**/*.entity.ts', __dirname + '/entity/**/*.entity.js'],
migrations: [__dirname + '/migration/**/*.ts', __dirname + '/migration/**/*.js'],

它对我有用并且应该有用。


-1
投票
  1. 确保您已配置:
  "migrations": ["db-migrations/*{.ts,.js}"],
  "cli": {
    "migrationsDir": "db-migrations"
  }
  1. 启用
    synchronize
    并转到上一个。您的架构状态(运行项目 - 这将重置数据库更改)
  2. 进行代码更改(从 git 应用它们或移动到更改的分支)
  3. 禁用
    synchronize
    并运行项目(以使其编译更改) - 它可能会运行失败,但没关系
  4. 运行
    typeorm migration:generate -n TestMigration
    就会成功

从那里离开

synchronize
禁用,只需在运行之前编译项目
migration:generate

© www.soinside.com 2019 - 2024. All rights reserved.