Cascade delete not working

I have this schema:
export const albums = pgTable('albums', {
id: uuid('id').notNull().primaryKey().defaultRandom(),
name: text('name').notNull().unique(),

gameId: uuid('game_id').notNull(),
});

export const albumsRelations = relations(albums, ({ one, many }) => ({
music: many(musics),
}));

export const musics = pgTable('musics', {
id: uuid('id').notNull().primaryKey().defaultRandom(),
name: text('name').notNull(),
url: text('url').notNull().unique(),

albumId: uuid('album_id')
.notNull()
.references(() => albums.id, { onDelete: 'cascade' }),
});

export const musicsRelations = relations(musics, ({ one, many }) => ({
album: one(albums, { fields: [musics.albumId], references: [albums.id] }),
musicsToAuthors: many(musicsToAuthors),
}));

export const authors = pgTable('authors', {
id: uuid('id').notNull().primaryKey().defaultRandom(),
name: text('name').notNull().unique(),
});

export const authorsRelations = relations(authors, ({ many }) => ({
musicsToAuthors: many(musicsToAuthors),
}));

// more in first comment
export const albums = pgTable('albums', {
id: uuid('id').notNull().primaryKey().defaultRandom(),
name: text('name').notNull().unique(),

gameId: uuid('game_id').notNull(),
});

export const albumsRelations = relations(albums, ({ one, many }) => ({
music: many(musics),
}));

export const musics = pgTable('musics', {
id: uuid('id').notNull().primaryKey().defaultRandom(),
name: text('name').notNull(),
url: text('url').notNull().unique(),

albumId: uuid('album_id')
.notNull()
.references(() => albums.id, { onDelete: 'cascade' }),
});

export const musicsRelations = relations(musics, ({ one, many }) => ({
album: one(albums, { fields: [musics.albumId], references: [albums.id] }),
musicsToAuthors: many(musicsToAuthors),
}));

export const authors = pgTable('authors', {
id: uuid('id').notNull().primaryKey().defaultRandom(),
name: text('name').notNull().unique(),
});

export const authorsRelations = relations(authors, ({ many }) => ({
musicsToAuthors: many(musicsToAuthors),
}));

// more in first comment
When I try to delete all the musics from an album I get this error: update or delete on table "musics" violates foreign key constraint "musics_to_authors_music_id_musics_id_fk" on table "musics_to_authors" Here's my delete: await db.delete(musics).where(eq(musics.albumId, albumId)) I don't understand what I'm doing wrong
5 Replies
Tibo
TiboOP8mo ago
End of schema:
export const musicsToAuthors = pgTable(
'musics_to_authors',
{
musicId: uuid('music_id')
.notNull()
.references(() => musics.id, {onDelete: 'cascade'}),
authorId: uuid('author_id')
.notNull()
.references(() => authors.id, {onDelete: 'cascade'}),
},
(t) => ({
pk: primaryKey(t.musicId, t.authorId),
}),
);

export const musicToAuthorRelations = relations(musicsToAuthors, ({ one }) => ({
music: one(musics, {
fields: [musicsToAuthors.musicId],
references: [musics.id],
}),
author: one(authors, {
fields: [musicsToAuthors.authorId],
references: [authors.id],
}),
}));
export const musicsToAuthors = pgTable(
'musics_to_authors',
{
musicId: uuid('music_id')
.notNull()
.references(() => musics.id, {onDelete: 'cascade'}),
authorId: uuid('author_id')
.notNull()
.references(() => authors.id, {onDelete: 'cascade'}),
},
(t) => ({
pk: primaryKey(t.musicId, t.authorId),
}),
);

export const musicToAuthorRelations = relations(musicsToAuthors, ({ one }) => ({
music: one(musics, {
fields: [musicsToAuthors.musicId],
references: [musics.id],
}),
author: one(authors, {
fields: [musicsToAuthors.authorId],
references: [authors.id],
}),
}));
Sillvva
Sillvva8mo ago
Did you only add the onDelete action to Drizzle and not migrate/push it to your db?
Tibo
TiboOP8mo ago
@Sillvva I did a push to my db
SandCat
SandCat8mo ago
Any solutions found ? i run into this also using bun:sqlite delete cascade not working
Tibo
TiboOP8mo ago
I ended up deleting my db and creating it again and I haven't tested it again sorry
Want results from more Discord servers?
Add your server