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

> 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)
npm run 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;
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
/// 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
1 Reply
wisie
wisieOP11mo ago
db/index.ts - connection is created here
import { Pool } from 'pg';
import { NodePgDatabase, drizzle } from 'drizzle-orm/node-postgres';
import 'dotenv/config';

import * as postSchema from './schema/post';
import * as userSchema from './schema/user';

import logger from '../utils/logger';

function main() {
if (
!process.env.DB_HOST ||
!process.env.DB_NAME ||
!process.env.DB_USER ||
!process.env.DB_PASSWORD
) {
throw new Error('Database credentials missing.');
}

return new Pool({
port: 5432,
host: process.env.DB_HOST,
database: process.env.DB_NAME,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
ssl: true,
});
}

export let connection: Pool;
try {
connection = main();
logger.info('Database connection successful');
} catch (err) {
logger.error(
`An error has occurred while connecting to the database:\n${err}\nThe app will be shut down.`
);
process.exit(1);
}

const combinedSchemas = {
...postSchema,
...userSchema,
};

export const db: NodePgDatabase<typeof combinedSchemas> = drizzle(connection, {
schema: combinedSchemas,
});
import { Pool } from 'pg';
import { NodePgDatabase, drizzle } from 'drizzle-orm/node-postgres';
import 'dotenv/config';

import * as postSchema from './schema/post';
import * as userSchema from './schema/user';

import logger from '../utils/logger';

function main() {
if (
!process.env.DB_HOST ||
!process.env.DB_NAME ||
!process.env.DB_USER ||
!process.env.DB_PASSWORD
) {
throw new Error('Database credentials missing.');
}

return new Pool({
port: 5432,
host: process.env.DB_HOST,
database: process.env.DB_NAME,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
ssl: true,
});
}

export let connection: Pool;
try {
connection = main();
logger.info('Database connection successful');
} catch (err) {
logger.error(
`An error has occurred while connecting to the database:\n${err}\nThe app will be shut down.`
);
process.exit(1);
}

const combinedSchemas = {
...postSchema,
...userSchema,
};

export const db: NodePgDatabase<typeof combinedSchemas> = drizzle(connection, {
schema: combinedSchemas,
});
(ran out of characters)
Want results from more Discord servers?
Add your server