DT
Drizzle Team•9mo ago
Yakusho

Migrations won't run

It might be because it's 2 AM, but I have been trying to change my project's structure now that it's growing bigger and bigger, and I decided to separate the schema into multiple files, so now I have UserModel.ts, ProjectModel.ts, etc... Then I made a schema.ts, where I tried this:
import * as user from './UserModel.ts'
import * as project from './ProjectModel.ts'
export const schema = {
...user,
...project,
}
import * as user from './UserModel.ts'
import * as project from './ProjectModel.ts'
export const schema = {
...user,
...project,
}
Which seemed to work because I got types, but whenever I run drizzle-kit generate:pg, it shows me "0 tables", and nothing is created, I tried exporting default, just exporting them all, nothing seems to work, any ideas? Probably gonna go to bed and figure it out but it's been driving me insane for the past hour or so
2 Replies
wprk14
wprk14•9mo ago
I think it's the spread operator that's messing you up here... I have a schema.ts file in my project that just looks like this:
export * from '../../modules/addresses/address.schema';
export * from '../../modules/assets/asset.schema';
export * from '../../modules/creditors/creditor.schema';
export * from '../../modules/expenses/expense.schema';
export * from '../../modules/addresses/address.schema';
export * from '../../modules/assets/asset.schema';
export * from '../../modules/creditors/creditor.schema';
export * from '../../modules/expenses/expense.schema';
And then my schemas look like this:
import { type InferInsertModel, type InferSelectModel } from 'drizzle-orm';
import { sql } from 'drizzle-orm';
import {
doublePrecision,
pgTable,
timestamp,
uuid,
varchar,
} from 'drizzle-orm/pg-core';

import { users } from '../users/user.schema';

export const expenses = pgTable('expenses', {
id: uuid('id')
.default(sql`gen_random_uuid()`)
.notNull()
.primaryKey(),
owner: uuid('owner')
.notNull()
.references(() => users.id),
name: varchar('name').notNull(),
amount: doublePrecision('amount').notNull(),
createdAt: timestamp('created_at', { mode: 'date' }).defaultNow().notNull(),
updatedAt: timestamp('updated_at', { mode: 'date' }),
});

export type ExpenseCreateSchema = InferInsertModel<typeof expenses>;
export type ExpenseUpdateSchema = InferInsertModel<typeof expenses>;
export type Expense = InferSelectModel<typeof expenses>;
import { type InferInsertModel, type InferSelectModel } from 'drizzle-orm';
import { sql } from 'drizzle-orm';
import {
doublePrecision,
pgTable,
timestamp,
uuid,
varchar,
} from 'drizzle-orm/pg-core';

import { users } from '../users/user.schema';

export const expenses = pgTable('expenses', {
id: uuid('id')
.default(sql`gen_random_uuid()`)
.notNull()
.primaryKey(),
owner: uuid('owner')
.notNull()
.references(() => users.id),
name: varchar('name').notNull(),
amount: doublePrecision('amount').notNull(),
createdAt: timestamp('created_at', { mode: 'date' }).defaultNow().notNull(),
updatedAt: timestamp('updated_at', { mode: 'date' }),
});

export type ExpenseCreateSchema = InferInsertModel<typeof expenses>;
export type ExpenseUpdateSchema = InferInsertModel<typeof expenses>;
export type Expense = InferSelectModel<typeof expenses>;
Hopefully you can compare that to your code and work backwards from there 🙂
Yakusho
YakushoOP•9mo ago
It was exactly that, it works now, thanks @wprk14!
Want results from more Discord servers?
Add your server