TAINCER
TAINCER
DTDrizzle Team
Created by TAINCER on 4/23/2024 in #help
Drizzle wants to truncate my tables when not needed
I have this schema:
export const users = sqliteTable('users', {
id: text('id').notNull().primaryKey(),
username: text('username').notNull(),
created_at: text('created_at').notNull().default(sql`CURRENT_TIMESTAMP`),
});
export const users = sqliteTable('users', {
id: text('id').notNull().primaryKey(),
username: text('username').notNull(),
created_at: text('created_at').notNull().default(sql`CURRENT_TIMESTAMP`),
});
I already have users in this table in production. Now I want to add a flag like this:
export const users = sqliteTable('users', {
id: text('id').notNull().primaryKey(),
username: text('username').notNull(),
my_new_flag: int('my_new_flag').notNull().default(0), // New column
created_at: text('created_at').notNull().default(sql`CURRENT_TIMESTAMP`),
});
export const users = sqliteTable('users', {
id: text('id').notNull().primaryKey(),
username: text('username').notNull(),
my_new_flag: int('my_new_flag').notNull().default(0), // New column
created_at: text('created_at').notNull().default(sql`CURRENT_TIMESTAMP`),
});
When I then run drizzle-kit push:sqlite (im using turso) it wants to truncate the table:
ALTER TABLE `users` RENAME TO `__old_push_users`;
CREATE TABLE `users` (
`id` text PRIMARY KEY NOT NULL,
`username` text NOT NULL,
`my_new_flag` integer DEFAULT 0 NOT NULL,
`created_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL
);

INSERT INTO "users" SELECT * FROM "__old_push_users";
DROP TABLE `__old_push_users`;

Warning Found data-loss statements:
· You're about to add not-null my_new_flag column without default value, which contains 3 items

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
ALTER TABLE `users` RENAME TO `__old_push_users`;
CREATE TABLE `users` (
`id` text PRIMARY KEY NOT NULL,
`username` text NOT NULL,
`my_new_flag` integer DEFAULT 0 NOT NULL,
`created_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL
);

INSERT INTO "users" SELECT * FROM "__old_push_users";
DROP TABLE `__old_push_users`;

Warning Found data-loss statements:
· You're about to add not-null my_new_flag column without default value, which contains 3 items

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
Am I missing something? To me this doesn't make sense.
2 replies
DTDrizzle Team
Created by TAINCER on 4/21/2024 in #help
Use schema definition as normal types
I want to use my schema definition as a normal type. Basically what .select().from() returns - plain object with the type. I need it for something like this: const result = (await fetch("http://127.0.0.1:8000")).json() as Promise<typeof my_schema[]>; I could of course define a type with the same schema as my db schema but then I have to update that whenever I update my schema, is there a better way?
3 replies