首页 > 解决方案 > 带有nestjs的Typeorm出现错误:运行迁移时在“NOT”处或附近出现语法错误

问题描述

嗨,我正在使用带有嵌套 js 的 typeorm。一切正常。迁移是按我的意愿创建的,但是当我尝试使主键具有 uuid 主键时出现问题 这是代码

import {MigrationInterface, QueryRunner, Table} from "typeorm";

export class StudentsResponseTable1616216067510 implements MigrationInterface {

    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.createTable(new Table({
            name: 'students',
            columns: [
                {
                    name: 'id',
                    type: 'varchar',
                    isGenerated: true,
                    generationStrategy: 'uuid',
                    isPrimary: true
                },
                {
                    name: 'feedback',
                    type: 'jsonb',
                }
            ]
        }))
    }

    public async down(queryRunner: QueryRunner): Promise<void> {
    }

}


我收到这个错误

  length: 92,
  severity: 'ERROR',
  code: '42601',
  detail: undefined,
  hint: undefined,
  position: '31',
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'scan.l',
  line: '1180',
  routine: 'scanner_yyerror',
  query: 'CREATE TABLE "students" ("id" NOT NULL DEFAULT uuid_generate_v4(), "feedback" jsonb NOT NULL, CONSTRAINT "PK_7d7f07271ad4ce999880713f05e" PRIMARY KEY ("id"))',
  parameters: []


感谢您抽出时间并提供解决方案

标签: node.jstypescriptnestjstypeorm

解决方案


如果要使用generationStrategy: 'uuid'列类型必须是'uuid'. 不幸的是,网络上有一些过时的信息表明您应该使用'varchar'. 这是错误的:你需要'uuid'.

此外,如果您还没有这样做,您必须在数据库上运行以下命令才能使用 uuid generationStrategy:

CREATE EXTENSION "uuid-ossp";

推荐阅读