question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Warn user on MySQL windows about case sensitivity

See original GitHub issue

Bug 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.

  1. Create an empty project and initialize prisma with npx prisma init
  2. 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
}
  1. 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

  1. 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
}
  1. Run prisma migrate dev --name edit-user-table and check the generated migrations.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:open
  • Created 2 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mahmoudalsofcommented, May 25, 2021

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.

0reactions
mahmoudalsofcommented, Sep 8, 2022

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

MySQL 8.0 Reference Manual :: 9.2.3 Identifier Case Sensitivity
Table and database names are stored on disk using the lettercase specified in the CREATE TABLE or CREATE DATABASE statement. Name comparisons are...
Read more >
Are table names in MySQL case sensitive? - Stack Overflow
Database and table names are not case sensitive in Windows, and case sensitive ... In MySQL, databases correspond to directories within the data...
Read more >
Case-insensitive and case-sensitive databases - IBM
Suppose the database operates in a case-insensitive environment (for example: Microsoft SQL), and the query result is in the same case as the...
Read more >
Windows (MySQL) migrate up doesn't work when table names ...
Windows (MySQL) migrate up doesn't work when table names are case sensitive #4712 ... We could also warn users with lower_case_table_names=1 as it...
Read more >
Fixing MySQL 1045 Error: Access Denied - Percona
MySQL 1045 error Access Denied triggers in the following cases: · 1) Connecting to wrong host: · 2) User does not exist: ·...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found