【问题标题】:TypeORM migrations no changes were foundTypeORM 迁移未发现任何更改
【发布时间】:2020-07-05 03:54:26
【问题描述】:

我正在尝试使用 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;

【问题讨论】:

  • 您是否尝试将export default config 替换为export = config

标签: node.js migration typeorm


【解决方案1】:

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

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

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

【讨论】:

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

    从那里离开synchronize禁用,在运行migration:generate之前编译项目

    【讨论】:

      猜你喜欢
      • 2021-11-13
      • 2021-08-02
      • 2023-01-13
      • 2020-08-12
      • 1970-01-01
      • 2021-05-23
      • 2021-10-17
      • 1970-01-01
      • 2021-07-01
      相关资源
      最近更新 更多