typeorm-auditing icon indicating copy to clipboard operation
typeorm-auditing copied to clipboard

Does not populate foreign key

Open manvapatel-rbc opened this issue 2 years ago • 8 comments

Hello, Thanks a lot for developing this...it is really helpful!

I noticed that the library does not populate the foreign key field.

I have a relation like this:

 @ManyToOne(
    () => UserEntity,
    user => user.waitlist,
  )
  @JoinColumn({ name: 'member_id' })
  @ApiProperty({ type: () => UserEntity })
  public user: UserEntity;

The member_id field is populated in the main entity but not in the audit entity.

Is this not supported? Do I need to use @BeforeInsert for this?

manvapatel-rbc avatar Nov 29 '22 22:11 manvapatel-rbc

Hello, @manvapatel-rbc This was not intended behavior. It's a bug, so I fixed it and released version 0.5.5 to npm. Thanks for reporting the problem. Please let me know if there are any other problems. 🙏

kibae avatar Nov 30 '22 16:11 kibae

Thank you! But I just tested out the new version and now I am getting this error

Error: DataSource is not set for this entity.
    at Function.getRepository (/Users/manvapatel/Desktop/be/node_modules/typeorm-auditing/src/repository/BaseEntity.ts:115:19)
    at Function.create (/Users/manvapatel/Desktop/be/node_modules/typeorm-auditing/src/repository/BaseEntity.ts:189:21)
    at AuditingSubscriber.<anonymous> (/Users/manvapatel/Desktop/be/node_modules/typeorm-auditing/dist/auditing-subscriber.js:36:43)
    at Generator.next (<anonymous>)
    at /Users/manvapatel/Desktop/be/node_modules/typeorm-auditing/dist/auditing-subscriber.js:14:71
    at new Promise (<anonymous>)
    at __awaiter (/Users/manvapatel/Desktop/be/node_modules/typeorm-auditing/dist/auditing-subscriber.js:10:12)
    at AuditingSubscriber.saveHistory (/Users/manvapatel/Desktop/be/node_modules/typeorm-auditing/dist/auditing-subscriber.js:29:16)
    at AuditingSubscriber.<anonymous> (/Users/manvapatel/Desktop/be/node_modules/typeorm-auditing/dist/auditing-subscriber.js:46:25)
    at Generator.next (<anonymous>)
    at /Users/manvapatel/Desktop/be/node_modules/typeorm-auditing/dist/auditing-subscriber.js:14:71
    at new Promise (<anonymous>)
    at __awaiter (/Users/manvapatel/Desktop/be/node_modules/typeorm-auditing/dist/auditing-subscriber.js:10:12)
    at AuditingSubscriber.afterInsert (/Users/manvapatel/Desktop/be/node_modules/typeorm-auditing/dist/auditing-subscriber.js:45:16)
    at /Users/manvapatel/Desktop/be/src/subscriber/Broadcaster.ts:169:56
    at Array.forEach (<anonymous>)

I have setup the data source properly and it works with the previous version.

manvapatel-rbc avatar Nov 30 '22 22:11 manvapatel-rbc

@manvapatel-rbc I published 0.5.6 version to npm. Can you check this?

If the problem persists, can you show me the entity definition and the code that caused the above error? 🙏

kibae avatar Dec 01 '22 00:12 kibae

@kibae Yes that worked! Thank you! Another thing I noticed was that it populated the foreign key properly on creation. But when I update the record, the new entry does not populate the foreign key. Which is fine for me...but I just wanted to bring that to your attention

manvapatel-rbc avatar Dec 01 '22 15:12 manvapatel-rbc

@manvapatel-rbc I think it's a bug. I'll try to find out where the problem is. Thank you. 🙏

kibae avatar Dec 02 '22 00:12 kibae

Hi pals. Any progress here?

lzkill avatar Jan 14 '23 01:01 lzkill

Hello, @lzkill @manvapatel-rbc

I checked through this test case and there was no problem when updating using BaseEntity and repository. Could you please show me the code where the problem occurs?

https://github.com/kibae/typeorm-auditing/commit/5e7b0829234354a3edf39b7dd215297a49cc1e72#diff-f62b2ebb388631b83b005b34aae44882d389aba552035ac581ec8e1559923b85R60

kibae avatar Jan 14 '23 02:01 kibae

Hi @kibae @lzkill I checked again and it seems to be working fine. I can't reproduce the issue I was having earlier

manvapatel-rbc avatar Jan 31 '23 16:01 manvapatel-rbc