drizzle-team / drizzle-team/drizzle-orm

[BUG]: Modifying a column causes WARNING DATA LOSS

Open
#2,047 12 comments 23 reactions 0 assignees View on GitHub
db/mysql db/postgres drizzle/kit improvement
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### What version of `drizzle-orm` are you using?

0.29.4

### What version of `drizzle-kit` are you using?

0.20.14

### Describe the Bug

When I change a column in the schema and then run the ‘drizzle-kit push:mysql’ command, I receive an error message saying that the migration will cause data loss.”

Schema File
image

I altered de column user_id from varchar(28) to varchar(30)

Warning message:
image

THIS ACTION WILL CAUSE DATA LOSS AND CANNOT BE REVERTED

Do you still want to push changes?
❯ No, abort
Yes, I want to truncate 1 table

### Expected behavior

The expected behavior was for the migration to be executed without requesting user action. I understand this to be a bug, as this migration does not cause any side effects on the database since the column was already nullable, and when I run the generated SQL directly on the database, no errors occur.

As shown:

Generated migration:
image

Running the generated SQL directly on the database:
image

### Environment & setup

Create schema:

```ts
export const assistants = mysqlTable(
"assistants",
{
id: varchar("id", { length: 29 })
.notNull()
.primaryKey()
.$defaultFn(() => createPrimaryKey("asst")),
abilities: json("abilities")
.$type()
.default([AssistantAbilities["TEXT"]]),
promptExamples: json("examples_prompt")
.$type<{ id: string; text: string }[]>()
.default([]),
createdBy: varchar("user_id", { length: 28 }).references(
() => users.id,
{ onDelete: "set null" },
),
createdAt: timestamp("created_at").defaultNow(),
updatedAt: timestamp("updated_at").defaultNow().onUpdateNow(),
},
(t) => ({
nameOrganizationUnique: unique().on(t.organizationId, t.name),
}),
);
```

Run commands:

`npx drizzle-kit generate:mysql`
`npx drizzle-kit push:mysql`

Alter schema:
```ts
export const assistants = mysqlTable(
"assistants",
{
id: varchar("id", { length: 29 })
.notNull()
.primaryKey()
.$defaultFn(() => createPrimaryKey("asst")),
abilities: json("abilities")
.$type()
.default([AssistantAbilities["TEXT"]]),
promptExamples: json("examples_prompt")
.$type<{ id: string; text: string }[]>()
.default([]),
createdBy: varchar("user_id", { length: 30 }).references(
() => users.id,
{ onDelete: "set null" },
),
createdAt: timestamp("created_at").defaultNow(),
updatedAt: timestamp("updated_at").defaultNow().onUpdateNow(),
},
(t) => ({
nameOrganizationUnique: unique().on(t.organizationId, t.name),
}),
);
```

Run commands:

`npx drizzle-kit generate:mysql`
`npx drizzle-kit push:mysql`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.