每当我运行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"
    ]
}


运行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正常工作(不要问为什么),因此请尝试运行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和typeorm migration:generate将创建ts文件。 migration:run和migration:revert命令仅适用于.js文件。因此,在运行命令之前,必须先编译打字稿文件。或者,您可以将ts-node与typeorm结合使用以运行.ts迁移文件。



资料来源:Link

关于node.js - TypeORM生成空迁移,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56924509/

10-09 20:44