Help Needed with Drizzlekit Migration Issue
I've encountered an issue where I manually deleted a table in Drizzlekit Studio using DROP TABLE "orderProduct". This was due to a bug inside my orderProduct where I had both an "orders" and "order" column, each referencing the "orders" table, and I couldn't remove them.
I'm not fully understanding how migrations work in Drizzlekit, which is why I'm reaching out. When I run bunx drizzle-kit generate and migrate, the table isn't being regenerated along with its relations.
Bunx drizzle-kit generate output:
14 tables
orderProduct 5 columns 0 indexes 3 fks
orders 7 columns 0 indexes 1 fks
.....
No schema changes, nothing to migrate 😴
Bunx drizzle-kit migrate output:
No config path provided, using default 'drizzle.config.ts'
Reading config file 'C:\...\drizzle.config.ts'
Using 'postgres' driver for database querying
[⣟] applying migrations...{
severity_local: 'NOTICE',
severity: 'NOTICE',
code: '42P06',
message: 'schema "drizzle" already exists, skipping',
file: 'schemacmds.c',
line: '132',
routine: 'CreateSchemaCommand'
}
{
severity_local: 'NOTICE',
severity: 'NOTICE',
code: '42P07',
message: 'relation "__drizzle_migrations" already exists, skipping',
file: 'parse_utilcmd.c',
line: '207',
routine: 'transformCreateStmt'
}
[â¡¿] applying migrations...PostgresError: relation "orderProduct" does not exist
severity_local: 'ERROR',
severity: 'ERROR',
code: '42P01',
file: 'namespace.c',
line: '434',
routine: 'RangeVarGetRelidExtended'
}
error: script "migrate" exited with code 1
How can I fix this? The schema clearly defines what should be generated. Any help would be greatly appreciated!1 Reply
Here's an image showing how it looked before:https://ibb.co/FYcxSG4
along with a snippet of the schema:
//Orders
export const orders = pgTable('orders', {
id: serial('id').primaryKey(),
userID: text('userID')
.notNull()
.references(() => users.id, { onDelete: 'cascade' }),
total: real('total').notNull(),
status: text('status').notNull(),
receiptURL: text('receiptURL'),
paymentIntentID: text('paymentIntentID'),
created: timestamp('created').defaultNow().notNull(),
});
export const orderProduct = pgTable('orderProduct', {
id: serial('id').primaryKey(),
quantity: real('quantity').notNull(),
variantID: serial('variantID')
.notNull()
.references(() => productVariants.id, { onDelete: 'cascade' }),
productID: serial('productID')
.notNull()
.references(() => products.id, { onDelete: 'cascade' }),
orderID: serial('orderID')
.notNull()
.references(() => orders.id, { onDelete: 'cascade' }),
// created: timestamp('created').defaultNow().notNull(),
});
//Orders relations
export const orderRelations = relations(orders, ({ one, many }) => ({
user: one(users, {
fields: [orders.userID],
references: [users.id],
relationName: 'orders',
}),
orderProducts: many(orderProduct, {
relationName: 'orderProduct',
}),
}));
export const orderProductRelations = relations(orderProduct, ({ one }) => ({
orders: one(orders, {
fields: [orderProduct.orderID],
references: [orders.id],
relationName: 'orderProduct',
}),
product: one(products, {
fields: [orderProduct.productID],
references: [products.id],
relationName: 'products',
}),
variant: one(productVariants, {
fields: [orderProduct.variantID],
references: [productVariants.id],
relationName: 'productVariants',
}),
}));