Replies: 4 comments
-
Please add a reproduction in order for us to be able to investigate. Depending on the quality of reproduction steps, this issue may be closed if no reproduction is provided. Why was this issue marked with the
|
Beta Was this translation helpful? Give feedback.
-
I've also just ran into this issue, I need a way to run side effects after a document is created and fully commited to the database |
Beta Was this translation helpful? Give feedback.
-
Can the first create be ran in it's own transaction, or disable transaction for that statement, per the docs: https://payloadcms.com/docs/database/transactions |
Beta Was this translation helpful? Give feedback.
-
Hey @ra7bi, your issue is related to transactions. Yes, the created document is not yet commited to the database in As from the docs:
To fix your issue, you need to pass the const schoolSettings = await req.payload.create({
collection: 'school-settings',
data: {
school: doc.id,
autoConfirmEnrollment: false,
timezone: 'UTC',
},
req
});
await req.payload.update({
collection: 'schools',
id: doc.id,
data: { 'school-settings': schoolSettings.id },
req
}) I'm converting this to a discussion as it's not an issue with Payload, let me know if you have any more questions. |
Beta Was this translation helpful? Give feedback.
-
Describe the Bug
Hi Payload team ,
I'm facing an issue with PostgreSQL where I need to create a related record after the parent record is created, but it seems the parent record isn’t fully committed when the action is triggered. This results in a foreign key constraint violation.
Hook
How can I ensure the parent record is fully committed before executing this related action? Any best practices or patterns for such scenarios?
Thanks!
Link to the code that reproduces this issue
pnpx create-payload-app@latest -t blank
Reproduction Steps
Create a schools record in the schools table.
Trigger an afterChange hook to create a related school-settings record with a foreign key referencing the schools record.
Observe that the hook attempts to create the school-settings record before the schools record is fully committed.
Encounter a foreign key constraint violation.
Which area(s) are affected? (Select all that apply)
db-postgres
Environment Info
Beta Was this translation helpful? Give feedback.
All reactions