-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Retrieving ID in @AfterInsert operation #3563
Comments
Did you use |
@vlapo When creating a So, its like the repository is not updated quickly enough |
Understand now. I thought But I do not understand why you need select person again? You have your entity in @AfterInsert()
public async handleAfterInsert() {
//Insert into Photo
const photo = New Photo();
photo.person = this;
await getConnection()
.getRepository(Photo)
.save(photo);
} |
@vlapo I see what you mean. When I just set Not sure why that's not working... |
Yeah you need use same @EventSubscriber()
export class PersonSubscriber implements EntitySubscriberInterface<Person> {
listenTo() {
return Person;
}
async afterInsert(event: InsertEvent<Person>) {
const photo = new Photo();
photo.person = event.entity;
await event.manager
.getRepository(Photo)
.save(photo);
}
} More info: #743 and #1705 (comment) |
@vlapo - Thanks a lot for your example. Forgive me, but I somehow still cannot get this to work. Nothing really happens when I insert a new I tried to read [https://github.com/typeorm/typeorm/blob/master/docs/listeners-and-subscribers.md#what-is-a-subscriber] as well. Seems to provide almost an identical example to yours. Do I need to create an instance of this class anywhere, or? Thanks for your patience |
You have to define subscribers in your connection options. Check this http://typeorm.io/#/connection-options, option I am really suprised we do not have any note about this option in https://github.com/typeorm/typeorm/blob/master/docs/listeners-and-subscribers.md. |
Worked like a charm - Thanks a lot @vlapo |
Issue type:
[X ] question
[ ] bug report
[ ] feature request
[ ] documentation issue
Database system/driver:
[ ]
cordova
[ ]
mongodb
[ ]
mssql
[X ]
mysql
/mariadb
[ ]
oracle
[ ]
postgres
[ ]
sqlite
[ ]
sqljs
[ ]
react-native
[ ]
expo
TypeORM version:
[X]
latest
[ ]
@next
[ ]
0.x.x
(or put your version here)Steps to reproduce or a small repository showing the problem:
I have two entities, Person and Photo which has a one-to-one relationship. After creating a person I'm trying to create a Photo in the
@AfterInsert
part. However, when trying to fetch the id of the newly created person, it returns undefined (The text was updated successfully, but these errors were encountered: