Skip to content
This repository has been archived by the owner on Mar 23, 2024. It is now read-only.

Foreign keys constraint clearing history records #779

Closed
predragffwd opened this issue Dec 23, 2021 · 5 comments
Closed

Foreign keys constraint clearing history records #779

predragffwd opened this issue Dec 23, 2021 · 5 comments

Comments

@predragffwd
Copy link

Hi, I have issue with cascade delete on ManyToOne (same as on OneToOne) relation. In my case I have entity with
@ManyToOne(() => UserData, { onDelete: 'CASCADE', nullable: false, }) @JoinColumn() data: UserData;

When making a history entity and extending my entity, same field is created in history, with cascade delete. Of course, when I delete UserData related record, mysql also removes all my records with related dataId, so I end up without my history records.

Can we have an option where I can disable all foreign keys in the history table?

@anchan828
Copy link
Owner

Hi @predragffwd,

For now, you need to create a migration file and call the dropUniqueIndices function.

See: https://github.com/anchan828/typeorm-helpers/tree/da14d2a6e8584ab049932547e5d050739d5134ca/packages/history#drop-unique-indices

Of course, you can also delete it manually. Please refer to this line.
See:

await queryRunner.dropForeignKey(metadata.tableName, dropForeignKeyName);

@predragffwd
Copy link
Author

Hi @anchan828,

This is fine, I do have migration that is doing exactly what you explained, but problem is that for development I have sync turned on, and when ever I do some changes it rebuilds the constraints and all indexes...

Also I had issues with unique values because history table is copying exactly the same entity table schema. Even if I drop all unique indexes with migration, sync would return them on change, and because I have duplicate records with the same values, it breaks on sync with duplicate entry error.

So migration works fine in case sync is turned off, for production it's perfect, but for development where I don't write entity migration on the start it's quite problematic as you see.

That is why it would be nice to define a configuration that will override the entity field configuration.

@anchan828
Copy link
Owner

anchan828 commented Jan 7, 2022

Hi @predragffwd,
The trouble with this problem is that the decorator of the inherited class is reused, and the decorator cannot be overwritten.
If you have a problem with unique indexes as in this case, it is better to create separate and independent classes instead of inheriting classes.

@Entity()
class TestEntity extends BaseEntity {
  @PrimaryGeneratedColumn()
  public id!: number;

  @Column()
  public test!: string;
}

@Entity()
class TestHistoryEntity extends TestEntity implements HistoryEntityInterface {
  @HistoryOriginalIdColumn()
  public originalID!: number;

  @HistoryActionColumn()
  public action!: HistoryActionType;
}

@Entity()
class TestEntity extends BaseEntity {
  @PrimaryGeneratedColumn()
  public id!: number;

  @Column()
  public test!: string;
}

@Entity()
class TestHistoryEntity extends BaseEntity implements HistoryEntityInterface {

  @PrimaryGeneratedColumn()
  public id!: number;

  @Column()
  public test!: string;

  @HistoryOriginalIdColumn()
  public originalID!: number;

  @HistoryActionColumn()
  public action!: HistoryActionType;
}

Currently, the History class always had to inherit from the original entity, but that restriction is meaningless. So we will try to remove this restriction.

@anchan828
Copy link
Owner

anchan828 commented Jan 7, 2022

I published @anchan828/typeorm-history@0.8.0

Creating a History class without class inheritance might work.

@Entity()
class TestEntity {
 @ManyToOne(() => UserData, { onDelete: 'CASCADE', nullable: false, })
 @JoinColumn()
 data: UserData;
}

@Entity()
class TestHistoryEntity implements HistoryEntityInterface {
 @ManyToOne(() => UserData, { createForeignKeyConstraints: false, nullable: false, })
 @JoinColumn()
 data: UserData;

 @HistoryOriginalIdColumn()
 public originalID!: number;

 @HistoryActionColumn()
 public action!: HistoryActionType;
}

@anchan828
Copy link
Owner

If you have any questions, please create new issue.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants