drizzle-team / drizzle-team/drizzle-orm
[BUG]: drizzle-kit/sqlite-core: Incorrect pragma usage causes rows to cascade during migration
- Dominant language
- TypeScript
- Stars
- 35.8k
- Forks
- 1.6k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 4
Description
### Report hasn't been filed before.
- [x] I have verified that the bug I'm about to report hasn't been filed before.
### What version of `drizzle-orm` are you using?
0.40.0
### What version of `drizzle-kit` are you using?
0.30.2
### Other packages
_No response_
### Describe the Bug
Generated migration for an updated table incorrectly toggles pragma, causing entire table to cascade.
Migration:
```
PRAGMA foreign_keys=OFF;--> statement-breakpoint
CREATE TABLE `__new_chat_characters` (
`chat_id` integer NOT NULL,
`character_id` integer,
`position` integer DEFAULT 0,
`is_active` integer DEFAULT true,
FOREIGN KEY (`chat_id`) REFERENCES `chats`(`id`) ON UPDATE no action ON DELETE cascade,
FOREIGN KEY (`character_id`) REFERENCES `characters`(`id`) ON UPDATE no action ON DELETE set null
);
--> statement-breakpoint
INSERT INTO `__new_chat_characters`("chat_id", "character_id", "position", "is_active") SELECT "chat_id", "character_id", "position", "is_active" FROM `chat_characters`;--> statement-breakpoint
DROP TABLE `chat_characters`;--> statement-breakpoint
ALTER TABLE `__new_chat_characters` RENAME TO `chat_characters`;--> statement-breakpoint
--> statement-breakpoint
CREATE TABLE `__new_chats` (
`id` integer PRIMARY KEY NOT NULL,
`name` text,
`is_group` integer DEFAULT false,
`user_id` integer NOT NULL,
`created_at` text,
`updated_at` text,
`scenario` text,
`metadata` text,
`group_reply_strategy` text DEFAULT 'ordered',
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
INSERT INTO `__new_chats`("id", "name", "is_group", "user_id", "created_at", "updated_at", "scenario", "metadata", "group_reply_strategy") SELECT "id", "name", "is_group", "user_id", "created_at", "updated_at", "scenario", "metadata", "group_reply_strategy" FROM `chats`;--> statement-breakpoint
DROP TABLE `chats`;--> statement-breakpoint
ALTER TABLE `__new_chats` RENAME TO `chats`;
--> statement-breakpoint
PRAGMA foreign_keys=ON;
```
Schema:
```
export const chatCharacters = sqliteTable("chat_characters", {
chatId: integer("chat_id")
.notNull()
.references(() => chats.id, { onDelete: "cascade" }),
characterId: integer("character_id")
.references(() => characters.id, { onDelete: "set null" }),
position: integer("position").default(0), // Position in the chat
isActive: integer("is_active", { mode: "boolean" }).default(true) // 1 if active in chat, 0 if not
})
export const chatCharactersRelations = relations(chatCharacters, ({ one }) => ({
chat: one(chats, {
fields: [chatCharacters.chatId],
references: [chats.id]
}),
character: one(characters, {
fields: [chatCharacters.characterId],
references: [characters.id]
})
}))
```
Contributor guide
Assessment
This issue has not been assessed yet.