0

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

1 Answer 1

1

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";
Sign up to request clarification or add additional context in comments.

1 Comment

i am using typeorm and when i change from varchar to uuid then the user table migrates successfully. But, when i apply foreign key to another table that references this then i receive error. so i switched back from columnType uuid to varchar

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.