Many to many relationship between one type
I'm making an application where I have users who are able to follow other users. This means that I need a many-to-many relationship, but both sides are the same type (a user). I'm not sure how to express this with drizzle's relations, as I'm not sure how to specify both sides of the relationship
For example...
In the example above, the UserRelations relation needs to have many follower relations twice, but it needs to be one for each side of the relationship. I'm not sure how to specify that these are different
12 Replies
You can specify the
relationName
parameter in the 2nd argument, like this:
It's not documented yet, unfortunately, but we'll try to add it to the documentation soondid you get this to work? I'm trying to implement the same thing, but I keep getting (in my case
friends
) friends table
is not defined in the schema. However I essentially just copied everything from here and it doesn't work :(. If you have any tips it'd be much appreciated
nvm I'm sorry to bother you... I forgot to export(using barrel files) it so I can actually query with it i guess bazar since I'm using relational queries and I wasn't importing my friends table because it was just saying friends: true
but interesting@minion3665 did you get this working? In your example above, you have
Following = pgTable("followers", {
the mismatch between Following and followers is confusing. Did you also have a separate "followings" table?
nvm got this working now, I had a typo in the relationName
(extra s in followings)were you guys able to get this to work? i get an error relation "following" or "followers" does not exist
Uncomment the blocks
Lmao they're commented because if I uncomment them it breaks
Will you be able to show me just the followers/following part of your schema? If not that's fine, what I thought of doing is, I'd just run raw SQL command to generate followers/following many to many relational tables and introspect it with drizzle kit
Yes I'll be home in about an hour
Thank you!
Instead of a "followings" table, I have a "relations" table that describes friendship + followerships, but the idea is the same:
users:
relations:
and an example query:
Oh this is advanced, I'll try recreating this as per my following/followers tomorrow, thanks!
this seems to be working, thank you!
np, did you figure out what you did wrong before
I did not, but I learned that having a many-to-many relation would be an advanced concept for me
I'm creating a blog application so I need to create relations for bookmarks etc
A post can be bookmarked by many, a user can bookmark many posts etc