TypeORM 生成空迁移

TypeORM generates an empty migration

每当我 运行 typeorm migration:generate -n NAME 时,我得到的只是一个错误,说明我没有对数据库进行任何更改。每当我 运行 typeorm migration:create -n NAME 时,我都会得到一个空的迁移文件。我所有的实体都在 ormconfig.json 文件中指定的文件夹中,并且是 .ts 格式。当 运行 执行 migration:generate 命令时,我收到一个与我的实体中的语法相关的错误(特别是我在文件顶部导入的位置)。

这是我的 ormconfig.json:

{
  "name": "default",
  "type": "postgres",
  "host": "localhost",
  "port": 5432,
  "username": "postgres",
  "password": "admin",
  "database": "classmarker",
  "synchronize": true,
  "logging": false,
  "entities": [
    "src/entity/*.ts"
  ],
  "subscribers": [
    "src/subscriber/*.ts"
  ],
  "migrations": [
    "src/migration/*.ts"
  ],
  "cli": {
    "entitiesDir": "src/entity",
    "migrationsDir": "src/migration",
    "subscribersDir": "src/subscriber"
  }
}

我的 package.json 包含以下软件包:

  "dependencies": {
    "@tsed/common": "^5.21.0",
    "@tsed/core": "^5.21.0",
    "@tsed/di": "^5.21.0",
    "@types/mssql": "^4.0.15",
    "@types/node": "^12.0.12",
    "body-parser": "^1.19.0",
    "compression": "^1.7.4",
    "concurrently": "^4.1.1",
    "cookie-parser": "^1.4.4",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "express-handlebars": "^3.1.0",
    "method-override": "^3.0.0",
    "reflect-metadata": "^0.1.12",
    "pg": "^7.11.0",
    "typeorm": "^0.2.15"
  },
  "devDependencies": {
    "@types/express": "^4.17.0",
    "@types/node": "^9.6.5",
    "dotenv": "^8.0.0",
    "nodemon": "^1.19.1",
    "ts-node": "^3.3.0",
    "typescript": "^3.3.3333"
  }

我的 tsconfig.json 看起来像这样:

{
    "version": "2.4.2",
    "compilerOptions": {
        "lib": ["es5", "es6"],
        "target": "es6",
        "module": "commonjs",
        "moduleResolution": "node",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "sourceMap": true
    },
    "exclude": [
        "node_modules"
    ]
}

我在 运行ning typeorm migration:generate -n Name:

时得到的错误
SyntaxError: Unexpected token import
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:616:28)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Function.PlatformTools.load (%AppData%\nvm\v8.11.2\node_modules\typeorm\platform\PlatformTools.js:107:28
当您尝试生成或 运行 .ts 格式的迁移时,

Unexpected token import 往往会出现(我认为它出现是因为您试图 import .ts 文件顶部的内容)。由于 TypeORM 与 .js 而不是 .ts 一起正常工作(不要问为什么),请尝试 运行ning ts-node ./node_modules/typeorm/cli.js migration:generate -n NAME 生成迁移并 ts-node ./node_modules/typeorm/cli.js migration:run 将其推送到数据库。

基本上,将这样的内容添加到您的 package.json:

中会更容易
    "add-migration": "ts-node ./node_modules/typeorm/cli.js migration:generate -n",
    "update-database": "ts-node ./node_modules/typeorm/cli.js migration:run"

然后你可以简单地 运行 他们使用 npm run add-migration -n NAMEnpm run update-database.

typeorm migration:create and typeorm migration:generate will create ts files. The migration:run and migration:revert commands only work on .js files. Thus the typescript files need to be compiled before running the commands. Alternatively you can use ts-node in conjunction with typeorm to run .ts migration files.