【问题标题】:TypeORM migrations wrong syntax when using PostgreSQL and isGenerated使用 PostgreSQL 和 isGenerated 时 TypeORM 迁移语法错误
【发布时间】:2021-01-15 12:49:49
【问题描述】:

我正在尝试修改已经在 MySQL 中完美运行的 TypeOrm 迁移脚本,以便与 PostgreSQL 一起使用。
我在使用生成的列创建表时卡住了:

{
  name: 'id',
  type: 'varchar',
  isPrimary: true,
  isGenerated: true,
  generationStrategy: 'uuid',
}

不知道为什么,但我得到了以下语法(我省略了工作部分):

CREATE TABLE "my_table_name" ("id" NOT NULL DEFAULT uuid_generate_v4(), <some other fields> , CONSTRAINT "<pk>" PRIMARY KEY ("id"))

迁移引发以下错误:error: syntax error at or near "NOT",我将其缩小为"id"NOT 之间没有类型声明的明显问题

如果我从配置中删除 isGenerated,则会出现类型并且一切正常,但没有 DEAFAULT uuid_generate_v4 部分。

我不确定可能是什么问题,非常感谢您的帮助

谢谢!

【问题讨论】:

    标签: mysql typescript postgresql typeorm


    【解决方案1】:

    当我们使用MYSQL时,我们需要知道NOT NULL不存在,IS NOT NULL存在。 所以你的代码将是 CREATE TABLE "my_table_name" ("id" IS NOT NULL DEFAULT uuid_generate_v4(), &lt;some other fields&gt; , CONSTRAINT "&lt;pk&gt;" PRIMARY KEY ("id")).

    https://www.techonthenet.com/mysql/is_not_null.php#:~:text=Example%20%2D%20With%20SELECT%20Statement,not%20contain%20a%20null%20value.

    【讨论】:

    • 迁移“SQL”正在由 Typeorm 生成。问题不在于 Mysql,而在于 Postgresql
    • 你能发布你的迁移吗?
    【解决方案2】:

    您的 id 类型必须为 'uuid' 以匹配生成策略,如下所示:

    {
      name: 'id',
      type: 'uuid',
      isPrimary: true,
      isGenerated: true,
      generationStrategy: 'uuid',
    }
    

    【讨论】:

      猜你喜欢
      • 2021-07-29
      • 2020-08-26
      • 1970-01-01
      • 1970-01-01
      • 2022-01-11
      • 2021-03-28
      • 2016-08-27
      • 2020-12-29
      • 2021-07-03
      相关资源
      最近更新 更多