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] A number value is specified as the default for a column of type char #404

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

Comments

@Karibash
Copy link

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 the default value of a column of type varchar is a numeric string, the default function is passed a numeric value instead of a string when the introspect command is used to generate the schema.

This is caused by casting a string to a number without checking whether the column is of type char.

const newColumn = {
  default: columnDefault === null ? void 0 : /^-?[\d.]+(?:e-?\d+)?$/.test(columnDefault) && !columnType.startsWith("decimal") ? Number(columnDefault) : isDefaultAnExpression ? clearDefaults(columnDefault, collation) : `'${columnDefault}'`,
  autoincrement: isAutoincrement,
  name: columnName,
  type: changedType,
  primaryKey: false,
  notNull: !isNullable,
  onUpdate
};

Expected behavior

If the column is a char type, the default is also specified as a string.

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..c0ecbb07b62c35b08b8ddf709a7b0a7b67e13aa7 100755
--- a/bin.cjs
+++ b/bin.cjs
@@ -15246,7 +15246,7 @@ We have encountered a collision between the index name on columns ${source_defau
           onUpdate = true;
         }
         const newColumn = {
-          default: columnDefault === null ? void 0 : /^-?[\d.]+(?:e-?\d+)?$/.test(columnDefault) && !columnType.startsWith("decimal") ? Number(columnDefault) : isDefaultAnExpression ? clearDefaults(columnDefault, collation) : `'${columnDefault}'`,
+          default: columnDefault === null ? void 0 : /^-?[\d.]+(?:e-?\d+)?$/.test(columnDefault) && !['decimal', 'char', 'varchar'].some(type => columnType.startsWith(type)) ? Number(columnDefault) : isDefaultAnExpression ? clearDefaults(columnDefault, collation) : `'${columnDefault}'`,
           autoincrement: isAutoincrement,
           name: columnName,
           type: changedType,
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