drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
21.7k stars 494 forks source link

[BUG]: Incorrect migration generation for indexes and constraints with schemas/databases in MySQL #2134

Open realmikesolo opened 3 months ago

realmikesolo commented 3 months ago

What version of drizzle-orm are you using?

0.30.7

What version of drizzle-kit are you using?

0.20.14

Describe the Bug

Drizzle kit generates incorrect migrations for tables with indexes and foreign key constraints within a specific schema in MySQL. The migrations are generated without fully specifying the table names with their respective schema names for indexes and constraints. This leads to errors when the migrations are applied to the database.

MySQL tables:

import { index, mysqlSchema, serial, text, varchar } from 'drizzle-orm/mysql-core';

export const customSchema = mysqlSchema('custom_schema');

export const users = customSchema.table(
  'users',
  {
    id: varchar('id', { length: 255 }).primaryKey(),
    name: text('name').notNull(),
    email: text('email').notNull(),
  },
  (t) => ({
    nameIndex: index('name_index').on(t.name),
  }),
);

export const posts = customSchema.table('posts', {
  id: serial('id').primaryKey(),
  title: text('title').notNull(),
  content: text('content').notNull(),
  userId: varchar('user_id', { length: 255 })
    .notNull()
    .references(() => users.id),
});

generates this SQL:

CREATE DATABASE `custom_schema`;
--> statement-breakpoint
CREATE TABLE `custom_schema`.`posts` (
    `id` serial AUTO_INCREMENT NOT NULL,
    `title` text NOT NULL,
    `content` text NOT NULL,
    `user_id` varchar(255) NOT NULL,
    CONSTRAINT `posts_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `custom_schema`.`users` (
    `id` varchar(255) NOT NULL,
    `name` text NOT NULL,
    `email` text NOT NULL,
    CONSTRAINT `users_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE INDEX `name_index` ON `users` (`name`);--> statement-breakpoint
ALTER TABLE `posts` ADD CONSTRAINT `posts_user_id_users_id_fk` FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE no action ON UPDATE no action;

The index is created on users instead of custom_schema.users, and the foreign key constraint references posts instead of custom_schema.posts. This causes the migration to fail with an error indicating that the table does not exist, as it attempts to reference tables in the default database schema instead of custom_schema.

// mysqltest is my default database
Error: Table 'mysqltest.users' doesn't exist

Expected behavior

Drizzle kit should generate migrations with schema names in references for indexes and constraints, ensuring operations are scoped to the correct schema/database.

CREATE INDEX `name_index` ON `custom_schema`.`users` (`name`); --> statement-breakpoint
ALTER TABLE `custom_schema`.`posts` ADD CONSTRAINT `posts_id_users_id_fk` FOREIGN KEY (`user_id`) REFERENCES `custom_schema`.`users`(`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;

Environment & setup

No response

tushargoyalofficial commented 1 month ago

I am on latest drizzle version, it's creating empty migration file with dropping off all tables for MySQL DB. Works fine if I don't use mysqlSchema.