Gabriel Colson
10/27/2020, 1:17 PMmodel User {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
relationships1 Relationship[] @relation("UserRelationship1")
relationships2 Relationship[] @relation("UserRelationship2")
}
model Relationship {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
user1 User @relation("UserRelationship1", fields: [user1Id], references: [id])
user1Id String
user2 User @relation("UserRelationship2", fields: [user2Id], references: [id])
user2Id String
}
Ryan
10/27/2020, 1:22 PMmodel User {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
friends Relationship[]
}
model Relationship {
id String @id @default(cuid())
user User? @relation(fields: [userId], references: [id])
userId String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
Gabriel Colson
10/27/2020, 1:34 PMRyan
10/27/2020, 1:36 PMRelationship
model.
For e.g. if user1
and user2
are friends, You can create a relationship and add user2
in the relationship of user1
and vice versa.Gabriel Colson
10/27/2020, 1:52 PMRelationship
to describe “the user a user is linked to” and it will also add a “back relation” to the User
model and it will end up exactly like the first one I sent. Or am I missing something?
What I am trying to do is a many-to-many self relation with a model in between so I can attach more data to the “edges” of the graphRyan
10/27/2020, 3:00 PMmodel User {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
friends Relationship[] @relation("friends")
}
model Relationship {
id String @id @default(cuid())
users User[] @relation("friends")
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
Instead of adding 2 separate fields, you can create this so that it can also be directly queried for.Gabriel Colson
10/27/2020, 3:57 PMibash
10/27/2020, 10:27 PMibash
10/28/2020, 12:40 AM-- have a table "OneWayRelationship"
-- with
-- fromUserId
-- toUserId
CREATE VIEW "Relationship" AS
SELECT "fromUserId" AS "fromUserId", "toUserId" AS "toUserId"
FROM "OneWayRelationship"
UNION
SELECT "toUserId" AS "fromUserId", "fromUserId" AS "toUserId"
FROM "OneWayRelationship"
Then the user model has link to relationships one wayibash
10/28/2020, 12:41 AMibash
10/28/2020, 12:51 AM