4

I am extending a class Entity to create a history table. The history table, must not inherit the unique constraints from the base class because we need to duplicate most of the data in there. Of course, I can just copy the properties without extending the base class but will result in maintaining two classes for each history table.

There is any way, to prevent TypeORM from generating constraints for a class entity?

I am thinking about something similar to avoiding foreign key constraint creation. Maybe a class decorator (something like @IgnoreConstraints) would be nice to have for this type of situations.

//Base class with required constraint
@Entity()
@Unique(['email', 'shopId'])
export class Customer {}

// Will inherit the unique constraint 
@Entity()
export class CustomerHistory extends Customer {}

More, I am using anchan828's very useful TypeORM History library which is adding a dropUniqueIndices method to be used in a later migration but unfortunately will later result in auto-generated migrations including those constraints and schema:sync will no longer work.

Thanks!

Andrew Radulescu
  • 1,862
  • 13
  • 21
  • 2
    you can create a `parent` class (ex. `CustomerBase`) with all the common fields and logic, also without the `@Entity` decorator. With this new class, you create the `Customer` and `CustomerHistory` entity classes which extends from the previously created class – Motakjuq Dec 07 '21 at 03:46
  • @Motakjuq this is a valid point and can be useful, thanks, but I am also looking forward to a database/ORM level solution – Andrew Radulescu Dec 08 '21 at 08:15

0 Answers0