每当我运行typeormigration:generate -n NAME
时,我得到的只是一个错误,表明我没有对数据库进行任何更改。每当我运行 typeormigration:create -n NAME
时,我都会得到一个空的迁移文件。我的所有实体都位于 ormconfig.json
文件中指定的文件夹中,并且采用 .ts 格式。运行迁移:生成命令时,我收到与实体中的语法相关的错误(特别是我在文件顶部导入的位置)。
这是我的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"
]
}
运行typeormigration: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 格式的迁移时,
意外的 token 导入
往往会出现(我认为它的出现是由于您尝试在 .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 NAME
和 npm 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.
- 来源:Link
关于node.js - TypeORM 生成空迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56924509/