Skip to content

Inconsistent table names in migrations #6962

Closed

Description

@macjuul

Bug description

The capitalization for generated table names appears to be inconsistent. When creating new tables, the migrations contain the capitalized and correct version of the table names, however altering the model causes an alter migration to be created with a fully lowercase version of the table name. This causes issues where migrate fails because it cannot find the right table.

Creation migration:
image

Alter migration:
image

This causes my production setup to break with The underlying table for model 'report' does not exist.

How to reproduce

  1. Create a migration adding a new table
  2. Create a migration that alters the previously created table
  3. The generated alter query will use a lowercase version of the table name

Expected behavior

The migration files should contain the actual model names, which in my case are capitalized

Environment & setup

  • OS: Windows 10
  • Database: MariaDB
  • Node.js version: v12.18.4
  • Prisma version:
prisma               : 2.22.0
@prisma/client       : 2.22.0
Current platform     : windows
Query Engine         : query-engine 60cc71d884972ab4e897f0277c4b84383dddaf6c (at node_modules\@prisma\engines\query-engine-windows.exe)
Migration Engine     : migration-engine-cli 60cc71d884972ab4e897f0277c4b84383dddaf6c (at  node_modules\@prisma\engines\migration-engine-windows.exe)
Introspection Engine : introspection-core 60cc71d884972ab4e897f0277c4b84383dddaf6c (at node_modules\@prisma\engines\introspection-engine-windows.exe)
Format Binary        : prisma-fmt 60cc71d884972ab4e897f0277c4b84383dddaf6c (at node_modules\@prisma\engines\prisma-fmt-windows.exe)
Default Engines Hash : 60cc71d884972ab4e897f0277c4b84383dddaf6c
Studio               : 0.379.0
Preview Features     : orderByRelation

Activity

  1. added
    bug/0-unknown
    kind/bug
    on May 6, 2021
  2. janpio commented on May 6, 2021

    @janpio
    Contributor

    Can you maybe show the full cycle - initial schema, generated migration file, adapted schema file (with modified model), and then the next migration file that uses the wrong casing? That would help us understand what is going on.

    MariaDB on Windows is most probably the culprit here, there are funny things going on with MySQL like databases on Windows quite often which we are partially already handling - probably that is one of the things we are not.

  3. macjuul commented on May 6, 2021

    @macjuul
    Author

    I've managed to reproduce it with a simple example

    Initial schema:

    generator client {
        provider        = "prisma-client-js"
        previewFeatures = ["orderByRelation"]
    }
    
    datasource db {
        provider = "mysql"
        url      = env("DATABASE_URL")
    }
    
    model User {
        id   Int    @id @default(autoincrement())
        name String
    }

    Which generates the following migration:

    -- CreateTable
    CREATE TABLE `User` (
        `id` INTEGER NOT NULL AUTO_INCREMENT,
        `name` VARCHAR(191) NOT NULL,
    
        PRIMARY KEY (`id`)
    ) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

    If I now add a new status field to my User model like so:

    model User {
        id     Int    @id @default(autoincrement())
        name   String
        status String @default("active")
    }

    The following migration will be generated, this time with a lowercase table name:

    -- AlterTable
    ALTER TABLE `user` ADD COLUMN `status` VARCHAR(191) NOT NULL DEFAULT 'active';
  4. pantharshit00 commented on May 6, 2021

    @pantharshit00
    Contributor

    Hmm I wasn't able to reproduce this. This is what I get when I generate the migration:

    ALTER TABLE `User` ADD COLUMN `status` VARCHAR(191) NOT NULL DEFAULT 'active';

    Can you please tell me whether your database is running inside of docker or it is running directly in Windows? MySQL on windows does have some text case related quirks so this might be related to that.

  5. janpio commented on May 6, 2021

    @janpio
    Contributor

    Maybe also see if you can share your MariaDB configuration, especially around things like https://mariadb.com/docs/reference/mdb/system-variables/lower_case_table_names/

    How is the table shown in some other database GUI (dbeaver etc) - lower or uppercase?

  6. macjuul commented on May 6, 2021

    @macjuul
    Author

    After some more testing it does actually look like my local Windows installation is handling the difference in capitalization correctly, however I'm only experiencing issues on my Ubuntu production server

    image

    While locally on Windows running SHOW TABLES returns all tables in lowercase, on Ubuntu it shows up capitalized. I did check the config files (there seem to be quite a few on ubuntu) however I could not find any reference to the setting, so presumably it is using the default value

  7. pantharshit00 commented on May 6, 2021

    @pantharshit00
    Contributor

    Yeah this is known, by default windows mariadb lowercase everything. I will suggest you run the database in docker for now for development so that it can match the production environment.

    You can also change the lower_case setting pointed out by @janpio to match the unix specification.

  8. macjuul commented on May 6, 2021

    @macjuul
    Author

    Running my local MariaDB in WSL has indeed solved the problem and generates correct migration files. Thanks!

  9. pantharshit00 commented on May 6, 2021

    @pantharshit00
    Contributor

    Closing in favour of #6972

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug/0-unknownkind/bugtopic: mariadbtopic: migratetopic: windows

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions