无法生成迁移

问题描述 投票:0回答:3

我有一个使用 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"
        }
    }

我有这个 User 实体类:

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)
node.js postgresql nestjs typeorm
3个回答
14
投票

我在 Windows 10 上遇到了这个问题,并且能够按照 这个答案这个评论修复它:

在我之前的

package.json
脚本中

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

我更换了 “打字”与

  "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>>


8
投票

从命令中删除

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

0
投票

谢谢 Rachit Kyte。它与我合作:

我将“typeorm”替换为

“脚本”:{ ... "typeorm:win": "ts-node -r tsconfig-paths/register node_modules ypeor

© www.soinside.com 2019 - 2024. All rights reserved.