BA
Better Auth•3d ago
Dustin

plugin schema: onDelete: "cascade" does not work?

Hi, I am creating my own plugin that creates groups within organizations. Part of my schema is this: group_member: { fields: { groupId: { type: "string", required: true, references: { model: "group", field: "id", onDelete: "cascade", }, }, memberId: { // This is supposed to be a organization member id, not a user ID!!! type: "string", required: true, references: { model: "member", field: "id", onDelete: "cascade", }, }, }, }, However, I cannot delete a group if it still has members. I am getting: # SERVER_ERROR: update or delete on table "group" violates foreign key constraint "group_member_groupId_fkey" on table "group_member" I would assume that onDelete: "cascade" just deletes the the group_member row?
14 Replies
🌠kkMihai ⚡
the onDelete: "cascade" will indeed delete rows from the group_member table when the group is deleted but its only active if the foreign key constraint is properly set up to cascade, the issue you're facing is likely because the memberId field is still linked to some records in the group_member table and the database doesnt know how to handle that when the group is deleted
Dustin
DustinOP•3d ago
memberId is linked to the member table of the organization plugin. The schema you are seeing is the group_member table. This table links members (of orgs) to groups (my plugin). If I delete a group, every row with groupId should be deleted, since this group can no longer have members. If I delete a member (remove a user from an org), all rows with memberId of this member should be deleted, since you can't be a member of a group of an org you are not a member of.
🌠kkMihai ⚡
hold on i m a bit confused so lost lmao @Dustin ye u need to set up cascading for both sides of the relation lmk
Dustin
DustinOP•3d ago
What do you mean? Ok other example
Ping
Ping•3d ago
* which adapter are you using? * What version of BA are you on? * Is your schema for DB generated by Better auth, and did you configure anything?
Dustin
DustinOP•3d ago
group: { fields: { organizationId: { type: "string", required: true, references: { model: "organization", field: "id", onDelete: "cascade", }, }, name: { type: "string", required: true, }, }, }, This is the group schema, pretty simple, only org ID and name It should delete the group on deleting the org, there are no member in the group or org. I am getting the same error. I cannot delete an org, when there is a group existing in it.
Dustin
DustinOP•3d ago
No description
Dustin
DustinOP•3d ago
Using Kysely Postgres, I am creating the migration with pnpx @better-auth/cli migrate --config ./server/utils/auth.ts, version is: 1.2.5, updating now I think the adapter, or something, is not creating the foreign keys correctly...
Dustin
DustinOP•3d ago
where dialect is the kysely dialect
No description
Dustin
DustinOP•3d ago
No description
Ping
Ping•3d ago
Yes that's likely correct. If I recall correctly, some adapter schema generation doesn't take into account FK onDeletes Do you have any migration files I can see?
Dustin
DustinOP•3d ago
There are no migration files. Better-auth just migrates 😄 How do I create a migration file I see, one sec yea, no on delete cascade create table "group" ("id" text not null primary key, "organizationId" text not null references "organization" ("id"), "name" text not null); create table "group_member" ("id" text not null primary key, "groupId" text not null references "group" ("id"), "memberId" text not null references "member" ("id"));
Ping
Ping•3d ago
For now you'll have to manually add that in, then push the migration files. This will be fixed in the future

Did you find this page helpful?