inserting many-to-many relationship correctly?
Hey,
I'm in the process of making the switch from Prisma to Drizzle and I want to make sure that I nail the basics, before messing up the more complex things.
My scenario:
- Users are authenticated using Supabase Auth
- Users can create clubs
- Users join as many clubs as they want
Thus I created the following schema:
3 Replies
When a (logged in) user creates a new club, I create a new row in the users table with the user's id. Since the users are stored in Supabase, I might not have an entry for them yet. That already seems redundant, but it was easier to start with:
That seems to work so far. Is this the correct approach?
Now, since I create an entry for the user every time, this will fail if the user creates a second club, since there's already a row for the user with the user's id. I would like to do a "find or create" logic for the user instead. I'm having issues with it though:
I'm getting:
Type 'RowList<never[]>' is not assignable to type '{ id: string; } | undefined'.ts(2322)For this line: Okay using
select
instead of query
and findFirst
seems to work better:
@floppydisk there is also
onConflictDoNothing
https://orm.drizzle.team/docs/insert#upserts-and-conflictsThank you! But since it's canceling the insert, it won't return anything, even if I were to use
returning
, right? Currently my code looks like this:
Not using returning
earlier was a mistake