0
It is possible to create the SQL of an Migration automatically, through the CLI command migration:generate -n MigrationName
?
However, it is possible to create Migrations from Migration API, more readable than pure SQL.
Example
Entities
@Entity('students')
class Student {
@PrimaryGeneratedColumn('uuid')
id: string;
@Column()
name: string;
}
@Entity('classes')
class Class {
@PrimaryGeneratedColumn('uuid')
id: string
@Column()
name: string;
@ManyToMany(() => Student)
@JoinTable()
students: Student[];
}
The problem is in creating relationships Manytomany with the Migration API, which is not described in the documentation.
Migration of Class using Migration API, without the relationship Manytomany
export class CreateClass implements MigrationInterface {
async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.createTable(new Table({
name: 'classes',
columns: [
{
name: 'id',
type: 'uuid',
isPrimary: true,
generationStrategy: 'uuid',
default: 'uuid_generate_v4()'
},
{
name: 'id',
type: 'varchar'
}
],
}))
}
}