Tibo
Tibo
Explore posts from servers
DTDrizzle Team
Created by Tibo on 4/18/2024 in #help
Cascade delete not working
I ended up deleting my db and creating it again and I haven't tested it again sorry
6 replies
DTDrizzle Team
Created by Tibo on 4/18/2024 in #help
Cascade delete not working
@Sillvva I did a push to my db
6 replies
DTDrizzle Team
Created by Tibo on 4/18/2024 in #help
Cascade delete not working
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],
}),
}));
6 replies
DTDrizzle Team
Created by Tibo on 3/29/2024 in #help
TypeError: Cannot read properties of undefined (reading 'compositePrimaryKeys')
When I do a push
3 replies
DTDrizzle Team
Created by Rodrigo on 3/21/2024 in #help
Unable to rename table that uses a composite key
@Rodrigo I have the same problem. Did you solve it ?
2 replies
DTDrizzle Team
Created by Tibo on 3/17/2024 in #help
How to do inserts in many-to-many relations ?
Here's how I handle the insert for now (it's not complete):
artists?.forEach(async (artist) => {
const artistExists = await db.query.authors.findFirst({
where: eq(authors.name, artist),
});
if (!artistExists) {
const newAuthor = await db
.insert(authors)
.values({
name: artist,
})
.returning({ authorId: authors.id });
const authorId = newAuthor[0].authorId;
}
});
const trackUrl = CLOUDFRONT_URL + filename;
await db.insert(musics).values({
title: metadata.common.title,
url: trackUrl,
duration: Math.round(metadata.format?.duration ?? 0),
number: metadata.common.track.no,
albumId,
});
artists?.forEach(async (artist) => {
const artistExists = await db.query.authors.findFirst({
where: eq(authors.name, artist),
});
if (!artistExists) {
const newAuthor = await db
.insert(authors)
.values({
name: artist,
})
.returning({ authorId: authors.id });
const authorId = newAuthor[0].authorId;
}
});
const trackUrl = CLOUDFRONT_URL + filename;
await db.insert(musics).values({
title: metadata.common.title,
url: trackUrl,
duration: Math.round(metadata.format?.duration ?? 0),
number: metadata.common.track.no,
albumId,
});
I'm using the music metadata. I'm currently looping over each artist and checking whether they exist. If they don't I'm creating them and I get their ID. This is where I'm stuck, as there are multiple tables for the relations I don't know in which one I should do the insert and if I'm using the right method. I guess that I need to get the author id and the music id and put them in one of the relation tables but I don't know if I should do that in the loop or outside and if there are risks that doing the insert in a loop could break it. Do I have the correct way of doing it or is there a better way ? How would you refactor my code ?
2 replies