An example of how dbPol work, so of course, what I can actually do here is to quickly show it. So assume I have an empty Prisma schema here, so far I only defined the data source. And if I now want to populate the Prisma schema with the models that adhere to the data source, I can just copy and paste it into the data source. That adhere to the tables that I already have in the database, I can run npx Prisma dbPol, it reads the structure of my database and creates the models for me.
The only thing that you should be aware of is that these relation fields, because they don't actually exist on the database, right? These are virtual fields that you only put in the Prisma schema to have a nicer Prisma client API when working with relations, but these don't exist in SQL. It will not infer any smart names for these, but only name them according to the type. So you probably often want to rename these after you ran a Prisma dbPol to make a little bit more sense and to fit the casing of your models otherwise.
All right, there is a question from Pratik about defining content as string and what's the limit for a string and int types? And this question is interesting because it cannot be answered just by looking at the Prisma schema because the Prisma types are only abstractions over the underlying data types of the database. So in this case in particular, if you're looking at the post, we see that this was just created as a text under the hood. So now you would have to go and look up in the SQLite documentation, how much characters are allowed for the text data type. And in fact, I recently learned this about SQLite. So it's special to SQLite that there are no varchars of a specific link, of a specific size. So if you use a SQLite database and you want to declare a column as varchar of like 30 characters or something like this, and then you run a migration, SQLite under the hood, well, actually also, it will accept the command because it accepts varchar as a keyword. But under the hood, it will just create a text for you. A text data type. For Postgres on the other hand, and MySQL and other databases, you can enrich the data types that you're declaring here in the Prisma schema with native type attributes that you can access like this. DB in this case is the name of the data source. And here you can now specify, for example, how long the title is supposed to be. And that way the constraint will be enforced when you try to create a post record in the database that has more than 256 characters.
All right, I think that all the questions from the chat are cleared. I would already suggest to move on with the next task, maybe a quick show of hand again, although, wait, I cannot hit anything with this. Okay, so quick show of hands again, who has already done with all. Quick note for those of you that ran into the issues of the null constraint violation on the fields author ID, I'm supposing, I'm guessing that you forgot the question mark right here. And that way you're making this field required. And what Prisma Migrate is trying to tell you here is that you're trying to add a required column to the database, but that would violate the consistency or the integrity of your data because you already have a post record in the database that has no field for author ID yet. So first you would have to go and update all the records that you have already in the database, set a value for them, and then you could run this migration. But the easier way is to just make this optional for you to circumvent this problem. I think that should be the issue, let me know if that's not.
Okay, issue from Derek about the new model not showing up an auto-complete. But you can execute QRIS against it. So that's probably then an issue with your VS code setup. I have seen some issues with that in the past and what usually helps is to restart the TypeScript server inside of VS code. And the way how I usually do this I use the, I like the command palette with command shift P and then I can type restart TypeScript. Why did they remove this? Maybe they renamed it since I last used it, restart dev server. No, interesting. I don't know why this not working anymore, why this is not possible. What else you can do is just to try and delete node modules and reinstall like node modules because that will also regenerate your client and maybe your Prisma types would be picked up then. So that's another thing that you could try, just the lead node modules run NPM install again and that should then hopefully fix your types. But yeah, it's an issue with kind of your VS code setup. Other question, do we always have to give a unique ID to find an entity in find unique? Yes, that's the entire point. Like find unique is a method that helps you to find a record in the database by any unique property. And as I mentioned before, that could either be one where you use the at unique attribute or also just the ID because it's implicit that this is unique. If you just want to have a random record, you can use find first, and that's when you don't have to provide a unique field.
All right, I would suggest that we move on with the next task or with the next lesson. And that being said in the next lessons, by the way, you will also again, write Prisma Client Query. So it's not the end of the world. If you didn't get through all the Prisma Client Queries here, you will now basically learn how Prisma Client integrates into a REST API or into a GraphQL API, kind of how it fits in. And for that, you are going to write Prisma Client Queries again. But instead of running them inside of a script, you'll add them in REST API routes and GraphQL resolvers. So let's go back to the overview page and move on to the next lesson.
Comments