node.js - 带有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: []
感谢您抽出时间并提供解决方案
解决方案
如果要使用generationStrategy: 'uuid'
列类型必须是'uuid'
. 不幸的是,网络上有一些过时的信息表明您应该使用'varchar'
. 这是错误的:你需要'uuid'
.
此外,如果您还没有这样做,您必须在数据库上运行以下命令才能使用 uuid generationStrategy:
CREATE EXTENSION "uuid-ossp";
推荐阅读
- android - Android Sum Quantity values from two Columns in two tables and update the Quantity value in one Column with the summed value
- javascript - 如何在 React Native 上使用 StackNavigator 与 DrawerNavigator 和 SwitchNavigator?
- python - 熊猫和集合 - ValueError:值的长度与索引的长度不匹配
- node.js - JWT 认证 - 防止 JWT 令牌被盗
- vue.js - 按显示的文本(不是模型)对 Vuetify 表进行排序
- sublimetext3 - Sublime text Build 3200 的 Unicode 支持使得无法选择变音符号并分别可视化它们
- node.js - 找不到快速路由器发布路由
- c - 从c中的文件中读取分号后的“未固定”整数个数
- excel - 不同文件的相同代码/功能(用户友好的方法)
- c - 使用 C 语言中的堆栈进行中缀到后缀转换