# Self-relations (Prisma ORM v6) (/docs/orm/v6/prisma-schema/data-model/relations/self-relations) > For the complete Prisma documentation index, see [llms.txt](https://www.prisma.io/docs/llms.txt). A markdown version of any docs page is available by appending `.md` to its URL. How to define and work with self-relations in Prisma. Location: ORM > v6 > Prisma Schema > Data Model > Relations > Self-relations A relation field can also reference its own model, in this case the relation is called a *self-relation*. Self-relations can be of any cardinality, 1-1, 1-n and m-n. **Questions answered in this page** * How do I model self-relations? * Do self-relations always require @relation? * How do 1-1 vs 1-n self-relations differ? Note that self-relations always require the `@relation` attribute. ## One-to-one self-relations [#one-to-one-self-relations] The following example models a one-to-one self-relation: #### Relational databases ```prisma model User { id Int @id @default(autoincrement()) name String? successorId Int? @unique successor User? @relation("BlogOwnerHistory", fields: [successorId], references: [id]) predecessor User? @relation("BlogOwnerHistory") } ``` #### MongoDB ```prisma model User { id String @id @default(auto()) @map("_id") @db.ObjectId name String? successorId String? @unique @db.ObjectId successor User? @relation("BlogOwnerHistory", fields: [successorId], references: [id]) predecessor User? @relation("BlogOwnerHistory") } ``` This relation expresses the following: * "a user can have one or zero predecessors" (for example, Sarah is Mary's predecessor as blog owner) * "a user can have one or zero successors" (for example, Mary is Sarah's successor as blog owner) > **Note**: One-to-one self-relations cannot be made required on both sides. One or both sides must be optional, otherwise it becomes impossible to create the first `User` record. To create a one-to-one self-relation: * Both sides of the relation must define a `@relation` attribute that share the same name - in this case, **BlogOwnerHistory**. * One relation field must be a [fully annotated](https://www.prisma.io/docs/orm/v6/prisma-schema/data-model/relations#relation-fields). In this example, the `successor` field defines both the `field` and `references` arguments. * One relation field must be backed by a foreign key. The `successor` field is backed by the `successorId` foreign key, which references a value in the `id` field. The `successorId` scalar relation field also requires a `@unique` attribute to guarantee a one-to-one relation. > **Note**: One-to-one self relations require two sides even if both sides are equal in the relationship. For example, to model a 'best friends' relation, you would need to create two relation fields: `bestfriend1` and a `bestfriend2`. Either side of the relation can be backed by a foreign key. In the previous example, repeated below, `successor` is backed by `successorId`: #### Relational databases ```prisma highlight=4;normal model User { id Int @id @default(autoincrement()) name String? successorId Int? @unique // [!code highlight] successor User? @relation("BlogOwnerHistory", fields: [successorId], references: [id]) predecessor User? @relation("BlogOwnerHistory") } ``` #### MongoDB ```prisma model User { id String @id @default(auto()) @map("_id") @db.ObjectId name String? successorId String? @unique @db.ObjectId // [!code highlight] successor User? @relation("BlogOwnerHistory", fields: [successorId], references: [id]) predecessor User? @relation("BlogOwnerHistory") } ``` Alternatively, you could rewrite this so that `predecessor` is backed by `predecessorId`: #### Relational databases ```prisma model User { id Int @id @default(autoincrement()) name String? successor User? @relation("BlogOwnerHistory") predecessorId Int? @unique // [!code highlight] predecessor User? @relation("BlogOwnerHistory", fields: [predecessorId], references: [id]) // [!code highlight] } ``` #### MongoDB ```prisma model User { id String @id @default(auto()) @map("_id") @db.ObjectId name String? successor User? @relation("BlogOwnerHistory") predecessorId String? @unique @db.ObjectId // [!code highlight] predecessor User? @relation("BlogOwnerHistory", fields: [predecessorId], references: [id]) // [!code highlight] } ``` No matter which side is backed by a foreign key, Prisma Client surfaces both the `predecessor` and `successor` fields: ```ts showLineNumbers const x = await prisma.user.create({ data: { name: "Bob McBob", successor: { // [!code highlight] connect: { id: 2, }, }, predecessor: { // [!code highlight] connect: { id: 4, }, }, }, }); ``` ### One-to-one self relations in the database [#one-to-one-self-relations-in-the-database] ### Relational databases [#relational-databases] In **relational databases only**, a one-to-one self-relation is represented by the following SQL: ```sql CREATE TABLE "User" ( id SERIAL PRIMARY KEY, "name" TEXT, "successorId" INTEGER ); ALTER TABLE "User" ADD CONSTRAINT fk_successor_user FOREIGN KEY ("successorId") REFERENCES "User" (id); ALTER TABLE "User" ADD CONSTRAINT successor_unique UNIQUE ("successorId"); ``` ### MongoDB [#mongodb] For MongoDB, Prisma ORM currently uses a [normalized data model design](https://www.mongodb.com/docs/manual/data-modeling/), which means that documents reference each other by ID in a similar way to relational databases. The following MongoDB documents represent a one-to-one self-relation between two users: ```json { "_id": { "$oid": "60d97df70080618f000e3ca9" }, "name": "Elsa the Elder" } ``` ```json { "_id": { "$oid": "60d97df70080618f000e3caa" }, "name": "Elsa", "successorId": { "$oid": "60d97df70080618f000e3ca9" } } ``` ## One-to-many self relations [#one-to-many-self-relations] A one-to-many self-relation looks as follows: #### Relational databases ```prisma model User { id Int @id @default(autoincrement()) name String? teacherId Int? teacher User? @relation("TeacherStudents", fields: [teacherId], references: [id]) students User[] @relation("TeacherStudents") } ``` #### MongoDB ```prisma model User { id String @id @default(auto()) @map("_id") @db.ObjectId name String? teacherId String? @db.ObjectId teacher User? @relation("TeacherStudents", fields: [teacherId], references: [id]) students User[] @relation("TeacherStudents") } ``` This relation expresses the following: * "a user has zero or one *teachers* " * "a user can have zero or more *students*" Note that you can also require each user to have a teacher by making the `teacher` field [required](https://www.prisma.io/docs/orm/v6/prisma-schema/data-model/models#optional-and-mandatory-fields). ### One-to-many self-relations in the database [#one-to-many-self-relations-in-the-database] ### Relational databases [#relational-databases-1] In relational databases, a one-to-many self-relation is represented by the following SQL: ```sql CREATE TABLE "User" ( id SERIAL PRIMARY KEY, "name" TEXT, "teacherId" INTEGER ); ALTER TABLE "User" ADD CONSTRAINT fk_teacherid_user FOREIGN KEY ("teacherId") REFERENCES "User" (id); ``` Notice the lack of `UNIQUE` constraint on `teacherId` - multiple students can have the same teacher. ### MongoDB [#mongodb-1] For MongoDB, Prisma ORM currently uses a [normalized data model design](https://www.mongodb.com/docs/manual/data-modeling/), which means that documents reference each other by ID in a similar way to relational databases. The following MongoDB documents represent a one-to-many self-relation between three users - one teacher and two students with the same `teacherId`: ```json { "_id": { "$oid": "60d9b9e600fe3d470079d6f9" }, "name": "Ms. Roberts" } ``` ```json { "_id": { "$oid": "60d9b9e600fe3d470079d6fa" }, "name": "Student 8", "teacherId": { "$oid": "60d9b9e600fe3d470079d6f9" } } ``` ```json { "_id": { "$oid": "60d9b9e600fe3d470079d6fb" }, "name": "Student 9", "teacherId": { "$oid": "60d9b9e600fe3d470079d6f9" } } ``` ## Many-to-many self relations [#many-to-many-self-relations] A many-to-many self-relation looks as follows: #### Relational databases ```prisma model User { id Int @id @default(autoincrement()) name String? followedBy User[] @relation("UserFollows") following User[] @relation("UserFollows") } ``` #### MongoDB ```prisma model User { id String @id @default(auto()) @map("_id") @db.ObjectId name String? followedBy User[] @relation("UserFollows", fields: [followedByIDs], references: [id]) followedByIDs String[] @db.ObjectId following User[] @relation("UserFollows", fields: [followingIDs], references: [id]) followingIDs String[] @db.ObjectId } ``` This relation expresses the following: * "a user can be followed by zero or more users" * "a user can follow zero or more users" Note that for relational databases, this many-to-many-relation is [implicit](https://www.prisma.io/docs/orm/v6/prisma-schema/data-model/relations/many-to-many-relations#implicit-many-to-many-relations). This means Prisma ORM maintains a [relation table](https://www.prisma.io/docs/orm/v6/prisma-schema/data-model/relations/many-to-many-relations#relation-tables) for it in the underlying database. If you need the relation to hold other fields, you can create an [explicit](https://www.prisma.io/docs/orm/v6/prisma-schema/data-model/relations/many-to-many-relations#explicit-many-to-many-relations) many-to-many self relation as well. The explicit version of the self relation shown previously is as follows: ```prisma model User { id Int @id @default(autoincrement()) name String? followedBy Follows[] @relation("followedBy") following Follows[] @relation("following") } model Follows { followedBy User @relation("followedBy", fields: [followedById], references: [id]) followedById Int following User @relation("following", fields: [followingId], references: [id]) followingId Int @@id([followingId, followedById]) } ``` ### Many-to-many self-relations in the database [#many-to-many-self-relations-in-the-database] ### Relational databases [#relational-databases-2] In relational databases, a many-to-many self-relation (implicit) is represented by the following SQL: ```sql CREATE TABLE "User" ( id integer DEFAULT nextval('"User_id_seq"'::regclass) PRIMARY KEY, name text ); CREATE TABLE "_UserFollows" ( "A" integer NOT NULL REFERENCES "User"(id) ON DELETE CASCADE ON UPDATE CASCADE, "B" integer NOT NULL REFERENCES "User"(id) ON DELETE CASCADE ON UPDATE CASCADE ); ``` ### MongoDB [#mongodb-2] For MongoDB, Prisma ORM currently uses a [normalized data model design](https://www.mongodb.com/docs/manual/data-modeling/), which means that documents reference each other by ID in a similar way to relational databases. The following MongoDB documents represent a many-to-many self-relation between five users - two users that follow `"Bob"`, and two users that follow him: ```json { "_id": { "$oid": "60d9866f00a3e930009a6cdd" }, "name": "Bob", "followedByIDs": [{ "$oid": "60d9866f00a3e930009a6cde" }, { "$oid": "60d9867000a3e930009a6cdf" }], "followingIDs": [{ "$oid": "60d9867000a3e930009a6ce0" }, { "$oid": "60d9867000a3e930009a6ce1" }] } ``` ```json { "_id": { "$oid": "60d9866f00a3e930009a6cde" }, "name": "Follower1", "followingIDs": [{ "$oid": "60d9866f00a3e930009a6cdd" }] } ``` ```json { "_id": { "$oid": "60d9867000a3e930009a6cdf" }, "name": "Follower2", "followingIDs": [{ "$oid": "60d9866f00a3e930009a6cdd" }] } ``` ```json { "_id": { "$oid": "60d9867000a3e930009a6ce0" }, "name": "CoolPerson1", "followedByIDs": [{ "$oid": "60d9866f00a3e930009a6cdd" }] } ``` ```json { "_id": { "$oid": "60d9867000a3e930009a6ce1" }, "name": "CoolPerson2", "followedByIDs": [{ "$oid": "60d9866f00a3e930009a6cdd" }] } ``` ## Defining multiple self-relations on the same model [#defining-multiple-self-relations-on-the-same-model] You can also define multiple self-relations on the same model at once. Taking all relations from the previous sections as example, you could define a `User` model as follows: #### Relational databases ```prisma model User { id Int @id @default(autoincrement()) name String? teacherId Int? teacher User? @relation("TeacherStudents", fields: [teacherId], references: [id]) students User[] @relation("TeacherStudents") followedBy User[] @relation("UserFollows") following User[] @relation("UserFollows") } ``` #### MongoDB ```prisma model User { id String @id @default(auto()) @map("_id") @db.ObjectId name String? teacherId String? @db.ObjectId teacher User? @relation("TeacherStudents", fields: [teacherId], references: [id]) students User[] @relation("TeacherStudents") followedBy User[] @relation("UserFollows", fields: [followedByIDs]) followedByIDs String[] @db.ObjectId following User[] @relation("UserFollows", fields: [followingIDs]) followingIDs String[] @db.ObjectId } ``` ## Related pages - [`Many-to-many relations`](https://www.prisma.io/docs/orm/v6/prisma-schema/data-model/relations/many-to-many-relations): How to define and work with many-to-many relations in Prisma. - [`One-to-many relations`](https://www.prisma.io/docs/orm/v6/prisma-schema/data-model/relations/one-to-many-relations): How to define and work with one-to-many relations in Prisma. - [`One-to-one relations`](https://www.prisma.io/docs/orm/v6/prisma-schema/data-model/relations/one-to-one-relations): How to define and work with one-to-one relations in Prisma. - [`Referential actions`](https://www.prisma.io/docs/orm/v6/prisma-schema/data-model/relations/referential-actions): Referential actions let you define the update and delete behavior of related models on the database level - [`Relation mode`](https://www.prisma.io/docs/orm/v6/prisma-schema/data-model/relations/relation-mode): Manage relations between records with relation modes in Prisma