typeORM 未发现数据库架构发生更改 - 无法生成迁移。要创建新的空迁移,请使用“typeormigration:create”命令

Die*_*ego 10 postgresql node.js npm typeorm

我正在做一门课程,我们使用express-node postgres和react(使用typescript)和typeORM。到目前为止,一切都运行良好,但我面临 typeORM 的问题,我不知道发生了什么以及为什么会发生此错误

\n

大问题:所以,问题是,我正在进行身份验证,并且在某些时候我们更改了数据库中的用户模式,因此我们必须进行迁移,但是,在迁移之前,我们做了,npm run typeorm schema:drop但是当我尝试时,这个发生在迁移过程中

\n

这是我使用的第一个命令

\n
npm run typeorm migration:generate -- --n create-users-table\n
Run Code Online (Sandbox Code Playgroud)\n

这是错误

\n
> new-typeorm-project@0.0.1 typeorm C:\\Users\\diego cifuentes\\Desktop\\Studying  Backend\\redit> ts-node ./node_modules/typeorm/cli.js "migration:generate" "create-users-table"\n\nbin.js migration:generate\n\nGenerates a new migration file with sql needs to be executed to update\nschema.\n\nOpciones:\n  -h, --help          Muestra ayuda                             [booleano]  -c, --connection    Name of the connection on which run a query.\n                                                      [defecto: "default"]  -n, --name          Name of the migration class.\n                                        [cadena de caracteres] [requerido]  -d, --dir           Directory where migration should be created.\n  -p, --pretty        Pretty-print generated SQL\n                                               [booleano] [defecto: false]  -f, --config        Name of the file with connection configuration.\n                                                    [defecto: "ormconfig"]  -o, --outputJs      Generate a migration file on Javascript instead of\n                      Typescript               [booleano] [defecto: false]      --dr, --dryrun  Prints out the contents of the migration instead of\n                      writing it to a file     [booleano] [defecto: false]      --ch, --check   Verifies that the current database is up to date and                      that no migrations are needed. Otherwise exits with\n                      code 1.                  [booleano] [defecto: false]  -v, --version       Muestra n\xc3\xbamero de versi\xc3\xb3n                 [booleano]\nFalta argumento requerido: n\nnpm ERR! code ELIFECYCLE\nnpm ERR! errno 1\nnpm ERR! new-typeorm-project@0.0.1 typeorm: `ts-node ./node_modules/typeorm/cli.js "migration:generate" "create-users-table"`\nnpm ERR! Exit status 1\nnpm ERR!\nnpm ERR! Failed at the new-typeorm-project@0.0.1 typeorm script.\nnpm ERR! This is probably not a problem with npm. There is likely additional logging output above.\n\nnpm ERR! A complete log of this run can be found in:\nnpm ERR!     C:\\Users\\diego cifuentes\\AppData\\Roaming\\npm-cache\\_logs\\2021-05-11T15_29_02_081Z-debug.log\n
Run Code Online (Sandbox Code Playgroud)\n

经过几个小时尝试到处寻找解决方案后,我将最后一个命令更改为这个命令

\n
npx typeorm migration:generate -- --n create-users-table\n
Run Code Online (Sandbox Code Playgroud)\n

这次的错误有所不同,看起来像这样

\n
No changes in database schema were found - cannot generate a migration. To create a new empty migration use "typeorm migration:create" command\n
Run Code Online (Sandbox Code Playgroud)\n

所以,我说,好吧,我越来越接近解决这个问题,但是......几个小时后(再次)寻找新的答案,我找不到任何东西,所以,我已经到了我需要的地步写这篇文章是为了解决我的问题。现在,让我向您展示我的 ormconfig、package.json 以及我想要在 postgres 数据库中迁移的实体。

\n

package.json(脚本typeorm在最后)

\n
{\n  "name": "new-typeorm-project",\n  "version": "0.0.1",\n  "description": "Awesome project developed with TypeORM.",\n  "devDependencies": {\n    "@types/bcrypt": "^5.0.0",\n    "@types/cookie": "^0.4.0",\n    "@types/cookie-parser": "^1.4.2",\n    "@types/express": "^4.17.11",\n    "@types/jsonwebtoken": "^8.5.1",\n    "@types/morgan": "^1.9.2",\n    "@types/node": "^15.0.2",\n    "morgan": "^1.10.0",\n    "nodemon": "^2.0.7",\n    "ts-node": "^9.1.1",\n    "typescript": "^4.2.4"\n  },\n  "dependencies": {\n    "bcrypt": "^5.0.1",\n    "class-transformer": "^0.4.0",\n    "class-validator": "^0.13.1",\n    "cookie": "^0.4.1",\n    "cookie-parser": "^1.4.5",\n    "dotenv": "^9.0.1",\n    "express": "^4.17.1",\n    "jsonwebtoken": "^8.5.1",\n    "pg": "^8.4.0",\n    "reflect-metadata": "^0.1.10",\n    "typeorm": "0.2.32"\n  },\n  "scripts": {\n    "start": "ts-node src/server.ts",\n    "dev": "nodemon --exec ts-node src/server.ts",\n    "typeorm": "ts-node ./node_modules/typeorm/cli.js"\n  }\n}\n\n
Run Code Online (Sandbox Code Playgroud)\n

ormconfing.json快速说明:我将实体、迁移和订阅者更改为 .js,因为使用 .ts 总是给我错误。

\n
{\n  "type": "postgres",\n  "host": "localhost",\n  "port": 5432,\n  "username": "**",\n  "password": "**",\n  "database": "**",\n  "synchronize": true,\n  "logging": true,\n  "entities": ["src/entities/**/*.js"],\n  "migrations": ["src/migrations/**/*.js"],\n  "subscribers": ["src/subscribers/**/*.js"],\n  "cli": {\n    "entitiesDir": "src/entities",\n    "migrationsDir": "src/migrations",\n    "subscribersDir": "src/subscribers"\n  }\n}\n\n
Run Code Online (Sandbox Code Playgroud)\n

我想向您展示的最后一件事是用户模式,尽管它可能不那么重要

\n
import {\n  Entity as TOEntity,\n  Column,\n  Index,\n  BeforeInsert,\n  OneToMany\n} from "typeorm";\nimport bcrypt from "bcrypt";\nimport { IsEmail, Length } from "class-validator";\nimport { Exclude } from "class-transformer";\n\nimport Entity from "./Entity";\n// import Post from "./Post";\n\n@TOEntity("users")\nexport default class User extends Entity {\n  constructor(user: Partial<User>) {\n    super();\n    Object.assign(this, user);\n  }\n\n  @Index()\n  @IsEmail()\n  @Column({ unique: true })\n  email: string;\n\n  @Index()\n  @Length(3, 200)\n  @Column({ unique: true })\n  username: string;\n\n  @Exclude()\n  @Length(6, 200)\n  @Column()\n  password: string;\n\n  // @OneToMany(() => Post, post => post.user)\n  // posts: Post[];\n\n  @BeforeInsert()\n  async hashedPassword() {\n    this.password = await bcrypt.hash(this.password, 6);\n  }\n}\n\n
Run Code Online (Sandbox Code Playgroud)\n

最终目标:所以,如果你能帮助我,你就能拯救我的生命。最后一件事是,我尝试先在 Stack Overflow 的西班牙语网站上发布我的问题,但没有人可以帮助我,所以也许这里有人会,感谢您的时间并保重!

\n

Era*_*han 10

entities,migrations和更改subscribers为您的dist目录。

例如:

  entities: ["dist/entities/**/*{.js,.ts}"],
  migrations: ["dist/migrations/**/*{.js,.ts}"],
  subscribers: ["dist/subscribers/**/*{.js,.ts}"],
Run Code Online (Sandbox Code Playgroud)

dist目录是构建应用程序时创建的目录。您可以使用-> ->找到您的dist目录。tsconfig.jsoncompilerOptionsoutDir

然后再次使用npm run build并尝试生成迁移来构建您的应用程序。