fix: set message_reference_id to null if original message is deleted

This commit is contained in:
CyberL1 2025-10-20 14:08:20 +02:00
parent ec8727dc6f
commit 8736343c4d
2 changed files with 20 additions and 2 deletions

View File

@ -169,7 +169,7 @@ export class Message extends BaseClass {
};
@JoinColumn({ name: "message_reference_id" })
@ManyToOne(() => Message)
@ManyToOne(() => Message, { onDelete: "SET NULL" })
referenced_message?: Message | null;
@Column({ type: "simple-json", nullable: true })
@ -272,7 +272,6 @@ export class Message extends BaseClass {
if (!opts.member_id) opts.member_id = message.author_id;
if (!opts.member) opts.member = await Member.findOneOrFail({ where: { id: opts.member_id! } });
if (!opts.guild) {
if (opts.guild_id) opts.guild = await Guild.findOneOrFail({ where: { id: opts.guild_id! } });
else if (opts.channel?.guild?.id) opts.guild = opts.channel.guild;

View File

@ -0,0 +1,19 @@
import { MigrationInterface, QueryRunner } from "typeorm";
export class MessageReferenceFix1760961911873 implements MigrationInterface {
name = "MessageReferenceFix1760961911873";
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "messages" DROP CONSTRAINT "FK_61a92bb65b302a76d9c1fcd3174"`);
await queryRunner.query(
`ALTER TABLE "messages" ADD CONSTRAINT "FK_61a92bb65b302a76d9c1fcd3174" FOREIGN KEY ("message_reference_id") REFERENCES "messages"("id") ON DELETE SET NULL ON UPDATE NO ACTION`,
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "messages" DROP CONSTRAINT "FK_61a92bb65b302a76d9c1fcd3174"`);
await queryRunner.query(
`ALTER TABLE "messages" ADD CONSTRAINT "FK_61a92bb65b302a76d9c1fcd3174" FOREIGN KEY ("message_reference_id") REFERENCES "messages"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
);
}
}