Warn user on MySQL windows about case sensitivity
See original GitHub issueBug description
I have been having issues figuring out why my migrations fail whenever I deploy to production.
I figured out that the generated migtations.sql
file does not use PascalCase when altering (for eg.) a table.
Running in my development environment, even if the generated file is in lowercase and the original table (inital migration) was in PascalCase, it runs fine and updates the database.
However, in production there seems to be an issue when running prisma migrate deploy
.
How to reproduce
I’ll reproduce as much as I can with code snippets where it makes sense for visual aid. I’ll be creating a table called User but this won’t matter obviously.
- Create an empty project and initialize prisma with
npx prisma init
- Create a table User and create three columns
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}
model User {
id String @id @default(uuid())
firstName String
lastName String
}
- Generate a migration file with
prisma migrate dev --name init-migration
and check the resulting output
-- CreateTable
CREATE TABLE `User` (
`id` VARCHAR(191) NOT NULL,
`firstName` VARCHAR(191) NOT NULL,
`lastName` VARCHAR(191) NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
Notice how the table name is PascalCase
- Amend something in your User table. I’ll add an email column
...
model User {
id String @id @default(uuid())
firstName String
lastName String
email String
}
- Run
prisma migrate dev --name edit-user-table
and check the generatedmigrations.sql
/*
Warnings:
- Added the required column `email` to the `User` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE `user` ADD COLUMN `email` VARCHAR(191) NOT NULL;
Ignoring the warning, we can see that now the user table is in lower case. This creates issues when deploying and will throw the following error:
Error: P3018
A migration failed to apply. New migrations can not be applied before the error is recovered from. Read more about how to resolve migration issues in a production database: https://pris.ly/d/migrate-resolve
Migration name: 20210524145658_edit_user_table
Database error code: 1146
Database error:
Table 'testdb.user' doesn't exist
Expected behavior
The generated migrations.sql
file that comes after the initial migrations file should have a consistent casing convention.
Instead of this generated migrations.sql
:
-- AlterTable
ALTER TABLE `user` ADD COLUMN `email` VARCHAR(191) NOT NULL;
it should be:
-- AlterTable
ALTER TABLE `User` ADD COLUMN `email` VARCHAR(191) NOT NULL;
Prisma information
Prisma Schema:
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}
model User {
id String @id @default(uuid())
firstName String
lastName String
email String
}
Environment & setup
- OS:
- Development: Windows 10 Pro
- Production: Ubuntu 20.04 (this is where the problem occurs)
- Database: MySQL
- Node.js version:
v15.6.0
Prisma Version
v2.19.0
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (5 by maintainers)
Top GitHub Comments
Thank you for the help. The documentation has helped. I changed my MySQL configurations to use
lower_case_table_names=2
and went through the steps above. All is good now. Hope there’s a good solution to this soon for us windows devs.Not sure if we need to close this but feel free to.
@nicolascalev
Honestly, it has been a while so my memory is a bit rusty. But IIRC, you can only do that when initially setting up the MySQL server. Setting the
lower_case_table_name=2
will only be useful in the environment you are deploying it to.However, there is a much easier way to accomplish this. I am assuming you want your model names in your Prisma schema to become lowercase and pluralized. You can do this directly in your
prisma.schema
file by making use of@map
Here is the documentation from Prisma about it.
I like this approach because it allows me to still conform with Prisma’s naming convention and also allows me to explicitly define my tables and column names casing.