import { MigrationInterface, QueryRunner, Table, TableForeignKey, TableIndex } from "typeorm"; /** * 创建残疾人本人电话表 * * 用于支持残疾人本人多个电话号码,与监护人电话表结构保持一致 * * 注:此迁移是手动创建的,因为 migration:generate 在此项目中不可靠 */ export class CreateDisabledPersonPhoneTable1768970000000 implements MigrationInterface { name = 'CreateDisabledPersonPhoneTable1768970000000'; public async up(queryRunner: QueryRunner): Promise { await queryRunner.createTable( new Table({ name: "disabled_person_phone", columns: [ { name: "id", type: "int", isPrimary: true, isGenerated: true, generationStrategy: "increment", comment: "本人电话ID", }, { name: "person_id", type: "int", isNullable: false, comment: "残疾人ID", }, { name: "phone_number", type: "varchar", length: "20", isNullable: false, comment: "本人电话号码", }, { name: "is_primary", type: "smallint", default: 0, isNullable: false, comment: "是否为主要联系电话:1-是,0-否", }, ], }), true ); // 创建外键约束 - 注意:disabled_person 表的主键是 person_id await queryRunner.createForeignKey( "disabled_person_phone", new TableForeignKey({ columnNames: ["person_id"], referencedColumnNames: ["person_id"], referencedTableName: "disabled_person", onDelete: "CASCADE", onUpdate: "CASCADE", }) ); // 创建索引 await queryRunner.createIndex( "disabled_person_phone", new TableIndex({ name: "idx_disabled_person_phone_person_id", columnNames: ["person_id"], }) ); } public async down(queryRunner: QueryRunner): Promise { await queryRunner.dropTable("disabled_person_phone", true); } }