【问题标题】:error: error: syntax error at or near "NOT" typeorm错误:错误:“NOT”类型或附近的语法错误
【发布时间】:2021-07-04 19:07:34
【问题描述】:

您好,我正在为我选择 Postgres 的数据库创建一个带有 NestJs 的应用程序,orm 是 TypeOrm,我正在创建我的迁移,迁移是一个文件,并且我已经配置了 package.json ,但是当我使用yarn typeorm migration:run 运行时,返回此错误:

query failed: CREATE TABLE "users" ("id" NOT NULL, "name" varchar NOT NULL, "email" varchar NOT NULL, "password" varchar NOT NULL, "biography" varchar NOT NULL, "links" varchar NOT NULL, "posts" int NOT NULL, "created_at" date NOT NULL DEFAULT now(), CONSTRAINT "UQ_97672ac88f789774dd47f7c8be3" UNIQUE ("email"), CONSTRAINT "posts" FOREIGN KEY ("posts") REFERENCES "posts" ("id") ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT "PK_a3ffb1c0c8416b9fc6f907b7433" PRIMARY KEY ("id"))
error: error: syntax error at or near "NOT"
    at Parser.parseErrorMessage (/home/luan/Área de Trabalho/Projetos/twinker/node_modules/pg-protocol/src/parser.ts:357:11)
    at Parser.handlePacket (/home/luan/Área de Trabalho/Projetos/twinker/node_modules/pg-protocol/src/parser.ts:186:21)
    at Parser.parse (/home/luan/Área de Trabalho/Projetos/twinker/node_modules/pg-protocol/src/parser.ts:101:30)
    at Socket.<anonymous> (/home/luan/Área de Trabalho/Projetos/twinker/node_modules/pg-protocol/src/index.ts:7:48)
    at Socket.emit (events.js:315:20)
    at Socket.EventEmitter.emit (domain.js:467:12)
    at addChunk (internal/streams/readable.js:309:12)
    at readableAddChunk (internal/streams/readable.js:284:9)
    at Socket.Readable.push (internal/streams/readable.js:223:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:188:23) {
  length: 92,
  severity: 'ERROR',
  code: '42601',
  detail: undefined,
  hint: undefined,
  position: '28',
  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: ROLLBACK
Error during migration run:
QueryFailedError: syntax error at or near "NOT"
    at new QueryFailedError (/home/luan/Área de Trabalho/Projetos/twinker/src/error/QueryFailedError.ts:9:9)
    at PostgresQueryRunner.<anonymous> (/home/luan/Área de Trabalho/Projetos/twinker/src/driver/postgres/PostgresQueryRunner.ts:228:19)
    at step (/home/luan/Área de Trabalho/Projetos/twinker/node_modules/typeorm/node_modules/tslib/tslib.js:143:27)
    at Object.throw (/home/luan/Área de Trabalho/Projetos/twinker/node_modules/typeorm/node_modules/tslib/tslib.js:124:57)
    at rejected (/home/luan/Área de Trabalho/Projetos/twinker/node_modules/typeorm/node_modules/tslib/tslib.js:115:69)
    at processTicksAndRejections (internal/process/task_queues.js:93:5) {
  length: 92,
  severity: 'ERROR',
  code: '42601',
  detail: undefined,
  hint: undefined,
  position: '28',
  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 "users" ("id" NOT NULL, "name" varchar NOT NULL, "email" varchar NOT NULL, "password" varchar NOT NULL, "biography" varchar NOT NULL, "links" varchar NOT NULL, "posts" int NOT NULL, "created_at" date NOT NULL DEFAULT now(), CONSTRAINT "UQ_97672ac88f789774dd47f7c8be3" UNIQUE ("email"), CONSTRAINT "posts" FOREIGN KEY ("posts") REFERENCES "posts" ("id") ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT "PK_a3ffb1c0c8416b9fc6f907b7433" PRIMARY KEY ("id"))',
  parameters: []
}

我的迁移代码太大所以放在这里:https://pastebin.com/Ww2d0F16

这是什么错误?我正在搜索,但没有找到任何有此错误的软件,您能帮帮我吗?

【问题讨论】:

  • 生成的 SQL 缺少列 id 的类型。 "id" NOT NULL 无效。对迁移不太了解,但这在您的代码中:type: 'id', 这是一个有效的类型吗?似乎没有翻译成一个。
  • 我认为它期待id 之后的类型?在我的 typeorm 迁移中,它看起来像 "id" character varying NOT NULL, ...
  • 我使用了 console.log(queryRunner.getMemorySql()) 来输出查询,然后运行每一个,直到我找到一个坏的。

标签: typescript postgresql nestjs typeorm


【解决方案1】:

type: 'id' 在 PosgresSQL 上不存在!

你可以试试这个=>

export class post1617813884949 implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.createTable(
      new Table({
        name: 'users',
        columns: [
          {
            name: 'id',
            type: 'number', // Number if your primary key is number else varchar if your primary key is uuid
            isPrimary: true,
            isGenerated: true,
            generationStrategy: 'increment',
          },
          {
            name: 'name',
            type: 'varchar',
          },
          {
            name: 'email',
            type: 'varchar',
            isUnique: true,
          },
          {
            name: 'password',
            type: 'varchar',
          },
          {
            name: 'biography',
            type: 'varchar',
          },
          {
            name: 'links',
            type: 'varchar',
          },
          {
            name: 'posts',
            type: 'int',
          },
          {
            name: 'created_at',
            type: 'date',
            default: 'now()',
          },
        ],
        foreignKeys: [
          {
            name: 'posts',
            columnNames: ['posts'],
            referencedTableName: 'posts',
            referencedColumnNames: ['id'],
            onUpdate: 'CASCADE',
            onDelete: 'CASCADE',
          },
        ],
      }),
    );
    ...other_table
}

【讨论】:

  • 直到今晚你的回答对我有所帮助,我已经有 2 周无法摆脱错误了。谢谢!
猜你喜欢
  • 2020-03-15
  • 2015-07-24
  • 2017-02-20
  • 1970-01-01
  • 2017-07-15
  • 2010-12-24
  • 2016-07-10
  • 2018-11-21
  • 1970-01-01
相关资源
最近更新 更多