You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hey first of all I've almost finished a production ready site with payloadcms thanks for open sourcing i think it's one of the best cmses I've ever used let me get to the issue currently there seems to be no way to set onUpdate or onDelete behaviors on relationships from collection configs meaning if you set a relationship to be required and try to delete the foreign key you will get this
[11:21:31] ERROR: nullvalueincolumn"user_id"ofrelation"comments"violatesnot-nullconstrainterr: {"type": "DatabaseError","message": "null value in column \"user_id\" of relation \"comments\" violates not-null constraint",//removed unnecessary lines for readability
How to reproduce ?
I will link the repo anyways but i don't think you need to reproduce it to understand whats wrong
Now try to delete the user of this comment and you will get the error
[11:21:31] ERROR: null value in column "user_id" of relation "comments" violates not-null constraint
err: {
"type": "DatabaseError",
"message": "null value in column "user_id" of relation "comments" violates not-null constraint",
//removed unnecessary lines for readability
Workaround for now
in payload.config.ts
db: postgresAdapter({pool: {connectionString: process.env.DATABASE_URI||'',},afterSchemaInit: [({ schema })=>{constrelations=['relations_comments']relations.forEach((relation)=>{constindex=Symbol.for(`drizzle:PgInlineForeignKeys`)//@ts-expect-error workaroundconstfkeys=schema.relations[relation].table[index]// Loop through the foreign keys and modify them//@ts-expect-error workaroundfkeys.forEach((foreignKey)=>{foreignKey.onDelete='CASCADE'foreignKey.onUpdate='CASCADE'})})returnschema},],}),
-create a user so you do not have to delete the admin user
-create a comment with user relation set to the newly created user
-try to delete the user it will fail
-uncomment the lines in payload.config.ts and restart the app
-try to delete now it will work
Which area(s) are affected? (Select all that apply)
Describe the Bug
Hey first of all I've almost finished a production ready site with payloadcms thanks for open sourcing i think it's one of the best cmses I've ever used let me get to the issue currently there seems to be no way to set onUpdate or onDelete behaviors on relationships from collection configs meaning if you set a relationship to be required and try to delete the foreign key you will get this
How to reproduce ?
I will link the repo anyways but i don't think you need to reproduce it to understand whats wrong
Here is an example
src/collections/Comments.ts
Now try to delete the user of this comment and you will get the error
Workaround for now
in payload.config.ts
Link to the code that reproduces this issue
https://github.com/uncor3/payload-issue
Reproduction Steps
-create a user so you do not have to delete the admin user
-create a comment with user relation set to the newly created user
-try to delete the user it will fail
-uncomment the lines in payload.config.ts and restart the app
-try to delete now it will work
Which area(s) are affected? (Select all that apply)
area: core
Environment Info
The text was updated successfully, but these errors were encountered: