diff --git a/drizzle-kit/src/serializer/mysqlSerializer.ts b/drizzle-kit/src/serializer/mysqlSerializer.ts index da52ac2fb..2a9d8f700 100644 --- a/drizzle-kit/src/serializer/mysqlSerializer.ts +++ b/drizzle-kit/src/serializer/mysqlSerializer.ts @@ -89,7 +89,9 @@ export const generateMySqlSnapshot = ( } if (column.isUnique) { - const existingUnique = uniqueConstraintObject[column.uniqueName!]; + const originalColumnName = getColumnCasing(column, undefined); + const uniqueName = column.uniqueName!.replace(originalColumnName, name); + const existingUnique = uniqueConstraintObject[uniqueName]; if (typeof existingUnique !== 'undefined') { console.log( `\n${ @@ -115,8 +117,8 @@ export const generateMySqlSnapshot = ( ); process.exit(1); } - uniqueConstraintObject[column.uniqueName!] = { - name: column.uniqueName!, + uniqueConstraintObject[uniqueName] = { + name: uniqueName, columns: [columnToSet.name], }; } diff --git a/drizzle-kit/src/serializer/sqliteSerializer.ts b/drizzle-kit/src/serializer/sqliteSerializer.ts index f1d28f759..336248d8b 100644 --- a/drizzle-kit/src/serializer/sqliteSerializer.ts +++ b/drizzle-kit/src/serializer/sqliteSerializer.ts @@ -90,7 +90,9 @@ export const generateSqliteSnapshot = ( columnsObject[name] = columnToSet; if (column.isUnique) { - const existingUnique = indexesObject[column.uniqueName!]; + const originalColumnName = getColumnCasing(column, undefined); + const uniqueName = column.uniqueName!.replace(originalColumnName, name); + const existingUnique = indexesObject[uniqueName]; if (typeof existingUnique !== 'undefined') { console.log( `\n${ @@ -116,8 +118,8 @@ export const generateSqliteSnapshot = ( ); process.exit(1); } - indexesObject[column.uniqueName!] = { - name: column.uniqueName!, + indexesObject[uniqueName] = { + name: uniqueName, columns: [columnToSet.name], isUnique: true, }; diff --git a/drizzle-kit/tests/mysql.test.ts b/drizzle-kit/tests/mysql.test.ts index b7e8cc1cf..45b9e9019 100644 --- a/drizzle-kit/tests/mysql.test.ts +++ b/drizzle-kit/tests/mysql.test.ts @@ -579,6 +579,7 @@ test('optional db aliases (snake case)', async () => { t1Col3: int().notNull(), t2Ref: int().notNull().references(() => t2.t2Id), t1Uni: int().notNull(), + t1NamelessUnique: int().notNull().unique(), t1UniIdx: int().notNull(), t1Idx: int().notNull(), }, @@ -627,9 +628,11 @@ test('optional db aliases (snake case)', async () => { \`t1_col3\` int NOT NULL, \`t2_ref\` int NOT NULL, \`t1_uni\` int NOT NULL, + \`t1_nameless_unique\` int NOT NULL, \`t1_uni_idx\` int NOT NULL, \`t1_idx\` int NOT NULL, CONSTRAINT \`t1_t1_id1\` PRIMARY KEY(\`t1_id1\`), + CONSTRAINT \`t1_t1_nameless_unique_unique\` UNIQUE(\`t1_nameless_unique\`), CONSTRAINT \`t1_uni\` UNIQUE(\`t1_uni\`), CONSTRAINT \`t1_uni_idx\` UNIQUE(\`t1_uni_idx\`) ); @@ -670,6 +673,7 @@ test('optional db aliases (camel case)', async () => { t1_col3: int().notNull(), t2_ref: int().notNull().references(() => t2.t2_id), t1_uni: int().notNull(), + t1_nameless_unique: int().notNull().unique(), t1_uni_idx: int().notNull(), t1_idx: int().notNull(), }, @@ -718,9 +722,11 @@ test('optional db aliases (camel case)', async () => { \`t1Col3\` int NOT NULL, \`t2Ref\` int NOT NULL, \`t1Uni\` int NOT NULL, + \`t1NamelessUnique\` int NOT NULL, \`t1UniIdx\` int NOT NULL, \`t1Idx\` int NOT NULL, CONSTRAINT \`t1_t1Id1\` PRIMARY KEY(\`t1Id1\`), + CONSTRAINT \`t1_t1NamelessUnique_unique\` UNIQUE(\`t1NamelessUnique\`), CONSTRAINT \`t1Uni\` UNIQUE(\`t1Uni\`), CONSTRAINT \`t1UniIdx\` UNIQUE(\`t1UniIdx\`) ); diff --git a/drizzle-kit/tests/sqlite-tables.test.ts b/drizzle-kit/tests/sqlite-tables.test.ts index 81ac7f100..29ac66e14 100644 --- a/drizzle-kit/tests/sqlite-tables.test.ts +++ b/drizzle-kit/tests/sqlite-tables.test.ts @@ -420,6 +420,7 @@ test('optional db aliases (snake case)', async () => { t2Ref: int().notNull().references(() => t2.t2Id), t1Uni: int().notNull(), t1UniIdx: int().notNull(), + t1NamelessUnique: int().notNull().unique(), t1Idx: int().notNull(), }, (table) => ({ @@ -468,31 +469,34 @@ test('optional db aliases (snake case)', async () => { \`t2_ref\` integer NOT NULL, \`t1_uni\` integer NOT NULL, \`t1_uni_idx\` integer NOT NULL, + \`t1_nameless_unique\` integer NOT NULL, \`t1_idx\` integer NOT NULL, FOREIGN KEY (\`t2_ref\`) REFERENCES \`t2\`(\`t2_id\`) ON UPDATE no action ON DELETE no action, FOREIGN KEY (\`t1_col2\`,\`t1_col3\`) REFERENCES \`t3\`(\`t3_id1\`,\`t3_id2\`) ON UPDATE no action ON DELETE no action ); `; - const st2 = `CREATE UNIQUE INDEX \`t1_uni_idx\` ON \`t1\` (\`t1_uni_idx\`);`; + const st2 = `CREATE UNIQUE INDEX \`t1_t1_nameless_unique_unique\` ON \`t1\` (\`t1_nameless_unique\`);`; - const st3 = `CREATE INDEX \`t1_idx\` ON \`t1\` (\`t1_idx\`);`; + const st3 = `CREATE UNIQUE INDEX \`t1_uni_idx\` ON \`t1\` (\`t1_uni_idx\`);`; - const st4 = `CREATE UNIQUE INDEX \`t1_uni\` ON \`t1\` (\`t1_uni\`);`; + const st4 = `CREATE INDEX \`t1_idx\` ON \`t1\` (\`t1_idx\`);`; - const st5 = `CREATE TABLE \`t2\` ( + const st5 = `CREATE UNIQUE INDEX \`t1_uni\` ON \`t1\` (\`t1_uni\`);`; + + const st6 = `CREATE TABLE \`t2\` ( \`t2_id\` integer PRIMARY KEY AUTOINCREMENT NOT NULL ); `; - const st6 = `CREATE TABLE \`t3\` ( + const st7 = `CREATE TABLE \`t3\` ( \`t3_id1\` integer, \`t3_id2\` integer, PRIMARY KEY(\`t3_id1\`, \`t3_id2\`) ); `; - expect(sqlStatements).toStrictEqual([st1, st2, st3, st4, st5, st6]); + expect(sqlStatements).toStrictEqual([st1, st2, st3, st4, st5, st6, st7]); }); test('optional db aliases (camel case)', async () => { @@ -506,6 +510,7 @@ test('optional db aliases (camel case)', async () => { t1_col3: int().notNull(), t2_ref: int().notNull().references(() => t2.t2_id), t1_uni: int().notNull(), + t1_nameless_unique: int().notNull().unique(), t1_uni_idx: int().notNull(), t1_idx: int().notNull(), }, @@ -554,6 +559,7 @@ test('optional db aliases (camel case)', async () => { \`t1Col3\` integer NOT NULL, \`t2Ref\` integer NOT NULL, \`t1Uni\` integer NOT NULL, + \`t1NamelessUnique\` integer NOT NULL, \`t1UniIdx\` integer NOT NULL, \`t1Idx\` integer NOT NULL, FOREIGN KEY (\`t2Ref\`) REFERENCES \`t2\`(\`t2Id\`) ON UPDATE no action ON DELETE no action, @@ -561,23 +567,25 @@ test('optional db aliases (camel case)', async () => { ); `; - const st2 = `CREATE UNIQUE INDEX \`t1UniIdx\` ON \`t1\` (\`t1UniIdx\`);`; + const st2 = `CREATE UNIQUE INDEX \`t1_t1NamelessUnique_unique\` ON \`t1\` (\`t1NamelessUnique\`);`; + + const st3 = `CREATE UNIQUE INDEX \`t1UniIdx\` ON \`t1\` (\`t1UniIdx\`);`; - const st3 = `CREATE INDEX \`t1Idx\` ON \`t1\` (\`t1Idx\`);`; + const st4 = `CREATE INDEX \`t1Idx\` ON \`t1\` (\`t1Idx\`);`; - const st4 = `CREATE UNIQUE INDEX \`t1Uni\` ON \`t1\` (\`t1Uni\`);`; + const st5 = `CREATE UNIQUE INDEX \`t1Uni\` ON \`t1\` (\`t1Uni\`);`; - const st5 = `CREATE TABLE \`t2\` ( + const st6 = `CREATE TABLE \`t2\` ( \`t2Id\` integer PRIMARY KEY AUTOINCREMENT NOT NULL ); `; - const st6 = `CREATE TABLE \`t3\` ( + const st7 = `CREATE TABLE \`t3\` ( \`t3Id1\` integer, \`t3Id2\` integer, PRIMARY KEY(\`t3Id1\`, \`t3Id2\`) ); `; - expect(sqlStatements).toStrictEqual([st1, st2, st3, st4, st5, st6]); + expect(sqlStatements).toStrictEqual([st1, st2, st3, st4, st5, st6, st7]); });