Check out a free preview of the full Build an AI-Powered Fullstack Next.js App, v3 course

The "Analysis Schema" Lesson is part of the full, Build an AI-Powered Fullstack Next.js App, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Scott creates the Analysis schema. An analysis contains the mood, summary, color, and a boolean value representing whether the entry is negative.

Preview
Close

Transcript from the "Analysis Schema" Lesson

[00:00:00]
>> Now, we need to make a model for our, we'll call it Analysis. This will be the AI analysis of a post. And we want that to be a separate model versus just storing it on the entry. So let's say Analysis. It's gonna have those same three things here at the top, just like so.

[00:00:24]
And this one's gonna have a relationship to a journal entry. So we'll just say journal, or actually I'll just call it entryId as a type String. And then I'll call this the entry, which is type JournalEntry. And it has a relationship of fields. That is gonna be entryId and a reference of id.

[00:00:56]
And then going back up to our journal entry. I just need to make the other connection. So this is not like the other one where it's one-to-many, this is one-to-one. An analysis has one journal entry and a journal entry has one analysis, so it's a one-to-one. So that's gonna look something like this.

[00:01:12]
So I can say analysis. You can call this whatever you want. I'll say analysis, and it's type is going to be Analysis. I have to put a question mark here. I have to make this optional. And that's because it is a one-to-one relationship. And you might have this chicken or egg problem of like, well, if this isn't optional, then you can't create a journal entry without the analysis being done.

[00:01:35]
But the analysis can't get created unless it has a journal entry id. So this has to be optional. Otherwise you won't be able to create any one of these due to a required restriction. And then lastly, we just need to go down here and we can say, index the entryId.

[00:01:54]
And this thing is freaking out because one-to-one must use unique, yeah, I'm sorry. This has to be unique if it's a one-to-one. So instead of saying index, we'll say unique like that. We'll probably have to change all of these to unique. You can't query without a unique index.

[00:02:15]
Which is great because that means if you're querying without index, you're scanning the whole table and that's bad. So it kinda forces you to do that, which is great. No more query planning, which is. I don't know if you ever had to do that before. It's not fun.

[00:02:34]
Cool, and then just the fields for the analysis. So analysis has a mood that's a String. It has a summary. That's a String of like, I'm just gonna put db.txt here just in case it's long. Who knows what the AI is gonna make it? So unpredictable. And then it also has a color.

[00:02:54]
That's a string, and then it has a Boolean on whether or not it was negative. That's a Boolean. Now we should have an error-free schema. Now if you wanna clean this up cuz your formatting is all junky, you can just go to the terminal and do npx prisma format, and it'll clean it up for you like that.

[00:03:19]
It'll actually go back and write these other side relationships for you as well. So you could just write the relationship on one side and then get that error and just do format and it'll go right on the other side for you. It just always picks a stupid name, so I never do it.

[00:03:34]
I don't know why it gave it that name. And it always capitalizes it. I'm like, why would I want this field to be capitalized? I don't know. Maybe I'm doing it wrong.
>> Do you happen to know what it does to comments that are in the file?
>> The comments?

[00:03:45]
>> Cuz I don't wanna to lose the notes I took.
>> No the comments, it doesn't touch the comments. You're good, yeah, you're fine. Cool, any other questions on this? All right, and whenever you make a change to your database just remember you gotta go back in your terminal npx prisma db push.

[00:04:05]
And, what is it saying? So this is really cool. This is why I like Prisma and PlanetScale. So it's basically saying, hey, you just added a few unique constraints to a model that you already previously had. So if you've already created some data with that model, you might lose some data or something's gonna be broke cuz you didn't add a default for those unique constraints.

[00:04:31]
So you might have some data loss here. Are you sure you wanna do this, ignore these warnings? So this is really great cuz I can't tell you how many times I've messed this up, writing migration files in Grunt. I know you remember that. [LAUGH] Or or something, and this is like, well, it worked on my machine.

[00:04:51]
And it's like, cuz I didn't have all that data on my machine, so of course it worked on my machine. So yeah, this just tells you that, yeah, I don't care, I don't have any users. So yeah, I do wanna ignore that warning, leave me alone. And yeah, it just does it.

[00:05:04]
So there we go, it syncs with the database. We have our new Prisma client, it's in sync. And just to verify, if we go back here, give it a second. And we should, there it is. So you can see, we have our analysis, our journal entry, and our user all ready to go.

[00:05:23]
And this is the sequel you did not have to write.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now