From c743118cad815c72da9f3354a2a6311a9951e587 Mon Sep 17 00:00:00 2001 From: Daniel Norman Date: Fri, 3 Apr 2020 20:17:46 +0200 Subject: [PATCH 1/2] Fix schema and add renaming --- .../02-start-from-scratch-sql.mdx | 61 ++++++++++++------- 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/content/01-getting-started/02-setup-prisma/02-start-from-scratch-sql.mdx b/content/01-getting-started/02-setup-prisma/02-start-from-scratch-sql.mdx index d049ccca67..e115593fea 100644 --- a/content/01-getting-started/02-setup-prisma/02-start-from-scratch-sql.mdx +++ b/content/01-getting-started/02-setup-prisma/02-start-from-scratch-sql.mdx @@ -223,15 +223,15 @@ CREATE TABLE "public"."Post" ( "createdAt" TIMESTAMP NOT NULL DEFAULT now(), content TEXT, published BOOLEAN NOT NULL DEFAULT false, - author INTEGER NOT NULL, - FOREIGN KEY (author) REFERENCES "public"."User"(id) + "authorId" INTEGER NOT NULL, + FOREIGN KEY ("authorId") REFERENCES "public"."User"(id) ); CREATE TABLE "public"."Profile" ( id SERIAL PRIMARY KEY NOT NULL, bio TEXT, - "user" INTEGER UNIQUE NOT NULL, - FOREIGN KEY ("user") REFERENCES "public"."User"(id) + "userId" INTEGER UNIQUE NOT NULL, + FOREIGN KEY ("userId") REFERENCES "public"."User"(id) ); ``` @@ -276,20 +276,20 @@ Great, you now created three tables in your database | Column name | Type | Primary key | Foreign key | Required | Default | | :---------- | :------------- | :---------- | :---------- | :------- | :----------------- | -| `id` | `SERIAL` | **✔️** | No | No | _autoincrementing_ | -| `createdAt` | `TIMESTAMP` | No | No | **✔️** | `now()` | -| `title` | `VARCHAR(255)` | No | No | **✔️** | - | +| `id` | `SERIAL` | **✔️** | No | No | _autoincrementing_ | +| `createdAt` | `TIMESTAMP` | No | No | **✔️** | `now()` | +| `title` | `VARCHAR(255)` | No | No | **✔️** | - | | `content` | `TEXT` | No | No | No | - | -| `published` | `BOOLEAN` | No | No | **✔️** | `false` | -| `author` | `INTEGER` | No | **✔️** | **✔️** | `false` | +| `published` | `BOOLEAN` | No | No | **✔️** | `false` | +| `authorId` | `INTEGER` | No | **✔️** | **✔️** | `false` | **Profile** | Column name | Type | Primary key | Foreign key | Required | Default | | :---------- | :-------- | :---------- | :---------- | :------- | :----------------- | -| `id` | `SERIAL` | **✔️** | No | No | _autoincrementing_ | -| `bio` | `TEXT` | No | No | **✔️** | - | -| `user` | `INTEGER` | No | **✔️** | **✔️** | `false` | +| `id` | `SERIAL` | **✔️** | No | No | _autoincrementing_ | +| `bio` | `TEXT` | No | No | **✔️** | - | +| `userId` | `INTEGER` | No | **✔️** | **✔️** | `false` | @@ -390,17 +390,15 @@ model Post { title String content String? published Boolean @default(false) - author User @relation(fields: [authorId], references: [id]) authorId Int - - @@index([authorId], name: "authorId") + User User @relation(fields: [authorId], references: [id]) } model Profile { - id Int @default(autoincrement()) @id - bio String? - user User @relation(fields: [userId], references: [id]) - userId Int @unique + id Int @default(autoincrement()) @id + bio String? + userId Int @unique + User User @relation(fields: [userId], references: [id]) } model User { @@ -421,9 +419,9 @@ Right now, there's a few minor "issues" with the data model: These changes are relevant for the generated Prisma Client API where using `posts` and `profile` will feel more natural and idiomatic to JavaScript/TypeScript developers. You can therefore [configure your Prisma Client API](../../reference/tools-and-interfaces/prisma-client/configuring-the-prisma-client-api). -Because both the generated `Post` and `Profile` fields are _virtual_ (i.e. they're _not backed by a foreign key in the database_), you can manually rename them in your Prisma schema: +Because both the generated `Post` and `Profile` in the `User` model fields are _virtual_ (i.e. they're _not backed by a foreign key in the database_), you can manually rename them in your Prisma schema: -```prisma copy +```prisma model User { id Int @default(autoincrement()) @id name String? @@ -433,6 +431,27 @@ model User { } ``` +Equally, apply rename following the cone Profile model, renaming `User` to `user` and the `Post` model, renaming `User` to field: + +```prisma +model Post { + id Int @default(autoincrement()) @id + createdAt DateTime @default(now()) + title String + content String? + published Boolean @default(false) + authorId Int + author User @relation(fields: [authorId], references: [id]) +} + +model Profile { + id Int @default(autoincrement()) @id + bio String? + userId Int @unique + user User @relation(fields: [userId], references: [id]) +} +``` + In this example, the database schema did follow the [naming conventions](../../reference/tools-and-interfaces/prisma-schema/models#naming-models) for Prisma models (only the virtual relation fields that were generated from introspection did not adhere to them and needed adjustment). This optimizes the ergonomics of the generated Prisma Client API. Learn more about this on the [Configuring your Prisma Client API](../../reference/tools-and-interfaces/prisma-client/configuring-the-prisma-client-api) page. From dc7a0188d055d27617bf66b1e307064eeb95e6c5 Mon Sep 17 00:00:00 2001 From: Daniel Norman Date: Mon, 6 Apr 2020 10:08:36 +0200 Subject: [PATCH 2/2] Correct default types --- .../02-setup-prisma/02-start-from-scratch-sql.mdx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/01-getting-started/02-setup-prisma/02-start-from-scratch-sql.mdx b/content/01-getting-started/02-setup-prisma/02-start-from-scratch-sql.mdx index e115593fea..984f2c2016 100644 --- a/content/01-getting-started/02-setup-prisma/02-start-from-scratch-sql.mdx +++ b/content/01-getting-started/02-setup-prisma/02-start-from-scratch-sql.mdx @@ -281,7 +281,7 @@ Great, you now created three tables in your database | `title` | `VARCHAR(255)` | No | No | **✔️** | - | | `content` | `TEXT` | No | No | No | - | | `published` | `BOOLEAN` | No | No | **✔️** | `false` | -| `authorId` | `INTEGER` | No | **✔️** | **✔️** | `false` | +| `authorId` | `INTEGER` | No | **✔️** | **✔️** | - | **Profile** @@ -289,7 +289,7 @@ Great, you now created three tables in your database | :---------- | :-------- | :---------- | :---------- | :------- | :----------------- | | `id` | `SERIAL` | **✔️** | No | No | _autoincrementing_ | | `bio` | `TEXT` | No | No | **✔️** | - | -| `userId` | `INTEGER` | No | **✔️** | **✔️** | `false` | +| `userId` | `INTEGER` | No | **✔️** | **✔️** | - |