[BUG]: migrations do not work - table already exists - ER_TABLE_EXISTS_ERROR - mysql
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
Fix Migration Error for Existing Tables in Drizzle ORM
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
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.
typescriptif (!await db.schema.hasTable('game_room_chat')) { await db.schema.createTable('game_room_chat', (table) => { /* table definition */ }); } - 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.
typescriptawait db.migrations.createMigrationTable(); - 3
Run Migrations with Verbose Logging
Execute the migration command with verbose logging to identify any skipped migrations or errors in the migration process.
bashbun run db/migrate.js --verbose - 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.
typescriptawait db.migrations.rollback(); await db.migrations.migrate(); - 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.
nonehttps://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
Alex Chen
2450 rep