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
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 deletedmemberId 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.
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
What do you mean?
Ok other example
* 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?
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.
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...where dialect is the kysely dialect


Yes that's likely correct. If I recall correctly, some adapter schema generation doesn't take into account FK
onDelete
s
Do you have any migration files I can see?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"));
For now you'll have to manually add that in, then push the migration files.
This will be fixed in the future