Drizzle TeamDT
Drizzle Team2y ago
1 reply
wisie

cannot generate migrations, getting a weird ReferenceError I don't know how to debug

Hi, I'm trying to generate migrations for my postgres database but I'm getting a very weird
ReferenceError: Cannot access 'users' before initialization
- I have no idea how to debug it, looking for help 🙏 🙏 🙏 🙏
Posting my db files below

Full log at the moment of the error:
npm run generate

> backend@1.0.0 generate
> drizzle-kit generate:pg --config=drizzle.config.ts

drizzle-kit: v0.20.14
drizzle-orm: v0.29.3

Reading config file 'G:\1010101010101\deployment_projects\FlutterNest\backend\drizzle.config.ts'
2024-02-09 17:05:24:524 info: Database connection successful
ReferenceError: Cannot access 'users' before initialization
    at Object.users (g:\1010101010101\deployment_projects\FlutterNest\backend\src\db\schema\user.ts:1:1)
    at Object.get [as users] (G:\1010101010101\deployment_projects\FlutterNest\backend\src\db\schema\user.ts:16:45) 
    at Object.get [as users] (G:\1010101010101\deployment_projects\FlutterNest\backend\src\db\index.ts:30:45)       
    at __spreadValues (G:\1010101010101\deployment_projects\FlutterNest\backend\src\db\index.ts:14:33)
    at Object.<anonymous> (g:\1010101010101\deployment_projects\FlutterNest\backend\src\db\index.ts:41:25)
    at Module._compile (node:internal/modules/cjs/loader:1241:14)
    at Module._compile (G:\1010101010101\deployment_projects\FlutterNest\backend\node_modules\drizzle-kit\bin.cjs:8644:30)
    at Module._extensions..js (node:internal/modules/cjs/loader:1295:10)
    at Object.newLoader [as .ts] (G:\1010101010101\deployment_projects\FlutterNest\backend\node_modules\drizzle-kit\bin.cjs:8648:13)
    at Module.load (node:internal/modules/cjs/loader:1091:32)


the command I'm using is
drizzle-kit generate:pg --config=drizzle.config.ts



drizzle.config.ts

import 'dotenv/config';
import type { Config } from 'drizzle-kit';

export default {
  schema: './src/db/schema/*',
  out: './src/db/migrations',
  driver: 'pg',
  dbCredentials: {
    connectionString: String(process.env.DB_URL),
  },
  verbose: true,
  strict: true,
} satisfies Config;


schema/user.ts


/// imports there

const userConfig = {
  minUsernameLength: 5,
  maxUsernameLength: 32,
  maxEmailLength: 256,
  maxPasswordLength: 128,
  minPasswordLength: 128,
  roleEnum: pgEnum('role', ['user', 'mod', 'admin']),
};

// User Schema
export const users = pgTable('users', {
  id: uuid('id').defaultRandom().primaryKey(),

  username: varchar('username', { length: userConfig.maxUsernameLength })
    .notNull()
    .unique(),

  email: varchar('email', { length: userConfig.maxEmailLength }).notNull(),

  password: varchar('password', {
    length: userConfig.maxPasswordLength,
  }).notNull(),

  lastPasswordChangeDate: timestamp('lastPasswordChangeDate'),
  createdAt: timestamp('createdAt').defaultNow().notNull(),
  birthDate: date('birthDate'),
  role: userConfig.roleEnum('role').default('user').notNull(),
  profilePicture: varchar('profilePicture').default('default.png'),
  passwordResetToken: varchar('passwordResetToken'),
  passwordResetExpires: date('passwordResetExpires'),
  active: boolean('active').default(true),
});

export type User = InferSelectModel<typeof users>;
export type NewUser = InferInsertModel<typeof users>;

// other schema utility functions
Was this page helpful?