How to add uuid as default value using migrations

See original GitHub issue

Issue type:

[x] question [ ] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [x] postgres [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[x] latest [ ] @next [ ] 0.x.x (or put your version here)

How could I add uuid_generate_v4() as a default value using typeorm migrations For example,

import { MigrationInterface, QueryRunner, Table } from 'typeorm'
import uuid = require('uuid')

export class InitUserTable1551883596067 implements MigrationInterface {

  public async up(queryRunner: QueryRunner): Promise<any> {
    await queryRunner.createTable(new Table({
      name: 'users',
      columns: [
        {
          name: 'id',
          type: 'uuid',
          isPrimary: true,
          isUnique: true,
          default: '??'
        }
      ]
    }), true)
  }

  public async down(queryRunner: QueryRunner): Promise<any> {
    await queryRunner.dropTable('users', true)
  }

}

If I used sql I would do it this way

CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
COMMIT;

CREATE TABLE users
(
    id         uuid DEFAULT uuid_generate_v4() PRIMARY KEY
);

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

68reactions
yuri-scarbaci-leniocommented, Mar 12, 2020

For anyone coming by this and using postgresql this is a way to get uuid generated ids

        // this ensure we can use default: `uuid_generate_v4()`
        await queryRunner.query(`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`);

        await queryRunner.createTable(new Table({
            name: "your_table",
            columns: [
                {
                    name: "id",
                    type: "uuid",
                    isPrimary: true,
                    isUnique: true,
                    generationStrategy: 'uuid',
                    default: `uuid_generate_v4()`
                },
            ]
        }), true);
4reactions
ikushlianskicommented, Sep 15, 2020

To make migrations also generate uuid you need the following definition in your model:

  @PrimaryGeneratedColumn('uuid', {
    name: 'product_id',
  })
  productId: string;

This generates a migration with IS NOT NULL DEFAULT uuid_generate_v4()

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using UUID as default value in a laravel migration
As I remember. UUID() is default function in mysql. See here mysqlserverteam.com/storing-uuid-values-in-mysql-tables For example, the request ...
Read more >
How to set a function as default in migration - Laracasts
For example I use the code bellow: $table->uuid('ID')->default('newid()'); I am using MS SQL database and I need to autogenerate the uuid itself, but...
Read more >
Symfony 5 - How to add default values when migrating - Reddit
Symfony 5 - How to add default values when migrating ? · select all IDs as an array · loop through array ·...
Read more >
#23932 (Document how to set a unique value for new fields ...
In my instance above, I am stating that I want a new uuid.uuid4 to be created as the default. This works GREAT for...
Read more >
typeorm uuid_generate_v4 | The AI Search Engine You Control
Also, if you don't need a primary column, but need to generate uuid sequence, ... typeorm/typeormHow to add uuid as default value using...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found