Skip to content

Commit

Permalink
docs(composite): add composite key docs (#968)
Browse files Browse the repository at this point in the history
  • Loading branch information
steebchen authored Sep 3, 2023
1 parent f2465f8 commit 7ea9c6b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/pages/docs/walkthrough/_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
"relations": "",
"raw": "",
"transactions": "",
"composite": "",
"limitations": ""
}
69 changes: 69 additions & 0 deletions docs/pages/docs/walkthrough/composite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Composite

## Composite queries

The examples use the following prisma schema:

```prisma
model Organization {
platformId String
platformKind String
name String
repositories Repository[]
@@unique(name: "organization_name", [platformId, name])
// Compose the primary key with the platformKind and platformId fields
@@id(name: "organization_id", [platformKind, platformId])
}
model Repository {
platformId String
platformKind String
orgId String?
org Organization? @relation(fields: [platformKind, orgId], references: [platformKind, platformId])
// Compose the primary key with the platformKind and platformId fields
@@id(name: "repository_id", [platformKind, platformId])
}
```

## Composite keys

You can query for composite keys using the composite key id, whether it is a unique or primary key.

```go
// query by primary key
org, err := client.Organization.FindUnique(
Organization.OrganizationID( // @@id(name: "organization_id", ...) maps to .OrganizationID
Organization.PlatformKind.Equals("private"),
Organization.PlatformID.Equals("123"),
),
).Exec(ctx)
```

```go
// query by unique key
org, err := client.Organization.FindUnique(
Organization.OrganizationName( // @@id(name: "organization_name", ...) maps to .OrganizationName
Organization.PlatformID.Equals("test"),
Organization.Name.Equals("test"),
),
).Exec(ctx)
```

## Create with composite primary keys

To create records with a composite primary key, just specify the fields in the correct order. You don't have to
explicitly specify the composite key name.

```go
org, err = client.Organization.CreateOne(
Organization.PlatformID.Set("123"),
Organization.PlatformKind.Set("private"),
Organization.Name.Set("test"),
).Exec(ctx)
```

0 comments on commit 7ea9c6b

Please sign in to comment.