Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] If the index name is the same as the generated name, it will be empty and a type error will occur #402

Open
Karibash opened this issue May 13, 2024 · 0 comments

Comments

@Karibash
Copy link

Karibash commented May 13, 2024

What version of drizzle-orm are you using?

0.30.10

What version of drizzle-kit are you using?

0.21.0

What type of database are you using?

MySQL

Describe the Bug

When generating a schema from an existing DB using the introspect command, the index name will be empty if it is identical to the generated name.

createTableIndexes2 = (tableName, idxs, casing2) => {
  let statement = "";
  idxs.forEach((it) => {
    let idxKey = it.name.startsWith(tableName) && it.name !== tableName ? it.name.slice(tableName.length + 1) : it.name;
    idxKey = idxKey.endsWith("_index") ? idxKey.slice(0, -"_index".length) + "_idx" : idxKey;
    idxKey = casing2(idxKey);
    const indexGeneratedName = indexName(tableName, it.columns);
    const escapedIndexName = indexGeneratedName === it.name ? "" : `"${it.name}"`;
    statement += `		${idxKey}: `;
    statement += it.isUnique ? "uniqueIndex(" : "index(";
    statement += `${escapedIndexName})`;
    statement += `.on(${it.columns.map((it2) => `table.${casing2(it2)}`).join(", ")}),`;
    statement += `
    `;
  });
  return statement;
};

However, since the index and uniqueIndex functions must take a string as an argument, a type error occurs.

https://github.com/drizzle-team/drizzle-orm/blob/a78eefe08e127922565486143e0150a718b27e8a/drizzle-orm/src/mysql-core/indexes.ts#L102-L108

Expected behavior

To avoid type errors, you must always pass the index name.

Patch

The following patch can be applied as a workaround until this issue is resolved.

diff --git a/bin.cjs b/bin.cjs
index fef2511d2e2d69c877fc925c281d88aa828f150f..b2bf7b5982518fda91b1ce21efd87e4eb41c58e3 100755
--- a/bin.cjs
+++ b/bin.cjs
@@ -108063,11 +108063,9 @@ import { sql } from "drizzle-orm"
         let idxKey = it.name.startsWith(tableName) && it.name !== tableName ? it.name.slice(tableName.length + 1) : it.name;
         idxKey = idxKey.endsWith("_index") ? idxKey.slice(0, -"_index".length) + "_idx" : idxKey;
         idxKey = casing2(idxKey);
-        const indexGeneratedName = indexName(tableName, it.columns);
-        const escapedIndexName = indexGeneratedName === it.name ? "" : `"${it.name}"`;
         statement += `		${idxKey}: `;
         statement += it.isUnique ? "uniqueIndex(" : "index(";
-        statement += `${escapedIndexName})`;
+        statement += `"${it.name}")`;
         statement += `.on(${it.columns.map((it2) => `table.${casing2(it2)}`).join(", ")}),`;
         statement += `
 `;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant