FG
๐Ÿ’ป Software๐Ÿ—„๏ธ Databases

[BUG]: migrations do not work - table already exists - ER_TABLE_EXISTS_ERROR - mysql

Fresh3 days ago
Mar 14, 20260 views
Confidence Score75%
75%

Problem

What version of `drizzle-orm` are you using? 0.30.10 What version of `drizzle-kit` are you using? 0.20.18 Describe the Bug whenever I run my package.json script: `"db:migrate": "bun run db/migrate.js",` my `migrate.js`: [code block] I get [code block] Expected behavior According to documentation the migrations should be skipped if these were already applied, this does not seem to be happening. ref: https://orm.drizzle.team/docs/migrations [code block] my migrations should be able to run and apply new changes to the schema without complaining about table already existing. I am very keen on solution to this bug and or workaround if such exists. Environment & setup _No response_

Error Output

error: Table 'game_room_chat' already exists

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix โ€“ Awaiting Verification

Fix Migration Error for Existing Tables in Drizzle ORM

Medium Risk

The error occurs because the migration script attempts to create a table that already exists in the database. This can happen if the migration logic does not properly check for existing tables before attempting to create them, or if the migration state is not correctly tracked.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Check Migration Logic

    Review the migration script to ensure it includes checks for existing tables before attempting to create them. Modify the migration to handle existing tables gracefully.

    typescript
    if (!await db.schema.hasTable('game_room_chat')) { await db.schema.createTable('game_room_chat', (table) => { /* table definition */ }); }
  2. 2

    Update Migration State Tracking

    Ensure that the migration state is being tracked correctly. If using a migration table, verify that it reflects the current state of applied migrations accurately.

    typescript
    await db.migrations.createMigrationTable();
  3. 3

    Run Migrations with Verbose Logging

    Execute the migration command with verbose logging to identify any skipped migrations or errors in the migration process.

    bash
    bun run db/migrate.js --verbose
  4. 4

    Rollback and Reapply Migrations

    If the issue persists, consider rolling back the last migration and reapplying it to ensure the schema is in sync with the migration state.

    typescript
    await db.migrations.rollback(); await db.migrations.migrate();
  5. 5

    Consult Documentation and Community

    If the problem continues, refer to the Drizzle ORM documentation for migration best practices and check community forums for similar issues.

    none
    https://orm.drizzle.team/docs/migrations

Validation

Confirm the fix by running the migration script again. Ensure that it completes without errors and that the schema reflects the expected changes without duplicating existing tables.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

drizzleormtypescriptbugdrizzle/kitdb/mysqlpriority