Reputation: 111
Hi there i am using typeorm with nest js. everything is working fine.Migrations are created as i want but there is a problem when i try to make a column primary having a primary key of uuid Here is the code
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> {
}
}
and i receive this error
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: []
Thank you for taking time and providing solution
Upvotes: 0
Views: 830
Reputation: 8596
If you want to use generationStrategy: 'uuid'
the column type must be 'uuid'
. Unfortunately there is some obsolete information on the web that states you should use 'varchar'
. This is wrong: you need 'uuid'
.
Also, if you haven't done so already, you must run the following command on your database to use uuid generationStrategy:
CREATE EXTENSION "uuid-ossp";
Upvotes: 1