How to add uuid as default value using migrations
See original GitHub issueIssue 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:
- Created 5 years ago
- Reactions:1
- Comments:5
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
For anyone coming by this and using postgresql this is a way to get uuid generated ids
To make migrations also generate
uuidyou need the following definition in your model:This generates a migration with
IS NOT NULL DEFAULT uuid_generate_v4()