-
Notifications
You must be signed in to change notification settings - Fork 2.1k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
Ensuring Record Commit Before Related Actions in PostgreSQL #10862
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
|
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 |
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 |
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. |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
The text was updated successfully, but these errors were encountered: