wisie
wisie
Explore posts from servers
DTDrizzle Team
Created by wisie on 8/28/2024 in #help
sql placeholder with an array throws an error
when executing this code:
const prepQuery = db
.select()
.from(table)
.where(inArray(table.postId, sql.placeholder("postIds")))
.prepare("prepquery");

const result = await prepQuery.execute({
postIds: ["esaeasdas", "esaeasdas", "esaeasdas"]
})
const prepQuery = db
.select()
.from(table)
.where(inArray(table.postId, sql.placeholder("postIds")))
.prepare("prepquery");

const result = await prepQuery.execute({
postIds: ["esaeasdas", "esaeasdas", "esaeasdas"]
})
Drizzle throws an error: syntax error at or near \"$1\. The code works when I execute the prepared query without the sql.placeholder, hardcoding an argument inside prepQuery - the error is not reproduced anyway else. Do I need to pass the array of strings there differently? I tried converting it into a string, but that does not work neither.
4 replies
DTDrizzle Team
Created by wisie on 2/9/2024 in #help
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
2 replies