无法生成迁移

时间:2019-08-22 03:46:28

标签: node.js postgresql nestjs typeorm

我有一个使用typeorm创建数据库的项目。当我执行命令以生成到我的项目的迁移时,它给了我错误

我有以下orm配置json文件:

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

我有这个用户实体类:

import { Entity, PrimaryGeneratedColumn, Column, OneToMany } from "typeorm";
import { Transaction } from "./Transactions";
export type UserStatus = "Active" | "Inactive";

@Entity()
export class User {

@PrimaryGeneratedColumn()
id: number;

@Column("varchar",{
    length:50
})
account_id: string;

@Column("varchar",{
    length: 50
})
name: string;

@Column("varchar",{
    length:50
})
email: string;

@Column("varchar",{
    length:15
})
phone_number: string;

@Column("varchar",{
    length:50
})
address: string;

@Column({
    type: "enum",
    enum: ["Active", "Inactive"]
}) status: UserStatus;


@Column("varchar",{
    length:50
})
current_id: string;

@OneToMany(type => Transaction, transaction => transaction.user)
transactions: Transaction[];

}

如果我执行命令以生成迁移,则会出现以下错误:

C:\Users\Firdaus97\source\repos\Cust Local payment\Cust Local payment\MyProject>ts-node ./node_modules/.bin/typeorm migrations:generate -n
C:\Users\Firdaus97\source\repos\Cust Local payment\Cust Local payment\MyProject\node_modules\.bin\typeorm:2
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
          ^^^^^^^

SyntaxError: missing ) after argument list
    at Module._compile (internal/modules/cjs/loader.js:720:22)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:788:10)
    at Module.load (internal/modules/cjs/loader.js:643:32)
    at Function.Module._load (internal/modules/cjs/loader.js:556:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:840:10)
    at Object.<anonymous> (C:\Users\Firdaus97\AppData\Roaming\npm\node_modules\ts-node\src\bin.ts:158:12)
    at Module._compile (internal/modules/cjs/loader.js:777:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:788:10)
    at Module.load (internal/modules/cjs/loader.js:643:32)
    at Function.Module._load (internal/modules/cjs/loader.js:556:12)

2 个答案:

答案 0 :(得分:3)

我在 Windows 10 上遇到了这个问题,并且能够按照 this answerthis comment 修复它:

在我之前的 package.json 脚本中

  "scripts": {
    ...
    "typeorm": "ts-node -r tsconfig-paths/register ./node_modules/.bin/typeorm",
    "migration:generate": "npm run typeorm migration:generate -- -n",
  }

我换了 “typeorm”与

  "scripts": {
    ...
    "typeorm:win": "ts-node -r tsconfig-paths/register node_modules\\typeorm\\cli.js",
    "migration:generate:win": "npm run typeorm:win migration:generate -- -n"
  }

然后这就像一个魅力npm run migration:generate:win -n <<migration name>>

答案 1 :(得分:1)

从您的命令中删除ts-node,然后重试:

.\node_modules\.bin\typeorm migrations:generate -n

typeorm在您的node_modules中已经是可执行文件,您不必使用ts-node,这仅意味着TypeScript文件。

其他解决方案:

  1. npm脚本:

在您的package.json中创建一个脚本:

{
  "scripts": {
    "migrate": "typeorm migrations:generate -n"
  }
}

使用npm运行它:

npm run migrate
  1. 使用npx

自npm版本5.2起,您可以执行以下操作:

npx typeorm migrations:generate -n
  1. 使用yarn
yarn typeorm migrations:generate -n