where clause in drizzle insert

await db
.insert(trackingQuestions)
.values({
questionNumber: questionNumber,
userId: session.user.id,
})
.where(
notExists(
db
.select()
.from(trackingQuestions)
.where(
and(
eq(trackingQuestions.questionNumber, questionNumber),
eq(trackingQuestions.userId, session.user.id)
)
)
)
);
await db
.insert(trackingQuestions)
.values({
questionNumber: questionNumber,
userId: session.user.id,
})
.where(
notExists(
db
.select()
.from(trackingQuestions)
.where(
and(
eq(trackingQuestions.questionNumber, questionNumber),
eq(trackingQuestions.userId, session.user.id)
)
)
)
);
can anyone tell why it's giving typescript error and how can i resolve it error: where clause doesn't exists i want to do this query with single query
4 Replies
whatplan
whatplan12mo ago
i am willing to help if you provide more information: please send the table schema, the typescript error you are getting and where it is showing up, and a explanation of what you are attempting to do with the query
Sylte
Sylte12mo ago
Instead of this where clause, another possibility could also be a unique index?
pradeep
pradeep12mo ago
i want do give below thing in one query:- if (condition) then don't insert in table else insert it into table
export const trackingQuestions = mysqlTable("tracking_questions", {
id: int("id").notNull().autoincrement().primaryKey(),
createdAt: timestamp("createdAt").defaultNow(),
questionNumber: int("questionNumber").notNull(),
userId: varchar("userId", { length: 255 }).notNull(),
});
export const trackingQuestions = mysqlTable("tracking_questions", {
id: int("id").notNull().autoincrement().primaryKey(),
createdAt: timestamp("createdAt").defaultNow(),
questionNumber: int("questionNumber").notNull(),
userId: varchar("userId", { length: 255 }).notNull(),
});
schema error is saying you cann't add where clause currently i am doing achiving this by
const questions = await db
.select()
.from(trackingQuestions)
.where(
and(
eq(trackingQuestions.questionNumber, questionNumber),
eq(trackingQuestions.userId, session.user.id)
)
);
if (questions.length === 0) {
await db.insert(trackingQuestions).values({
questionNumber: questionNumber,
userId: session.user.id,
});
}
const questions = await db
.select()
.from(trackingQuestions)
.where(
and(
eq(trackingQuestions.questionNumber, questionNumber),
eq(trackingQuestions.userId, session.user.id)
)
);
if (questions.length === 0) {
await db.insert(trackingQuestions).values({
questionNumber: questionNumber,
userId: session.user.id,
});
}
whatplan
whatplan12mo ago
oh I see yea I would try to avoid the nested db calls no reason to not seperate the reading data then writing data other than to make things more confusing
Want results from more Discord servers?
Add your server
More Posts
Secure way of previewing PDF in react app (without storing locally in readable way)I have a client that wants to securely preview PDF on a public PC. Normally, PDF is generated on a Prisma Transaction not executing properly in a Production environmentI have a Heavy Prisma transaction that skips the Promise.all portion in production in other computerTailwind compiler not working properly with TurborepoI am using @marminge 's acme-corp as a template and I have a UI component which is pretty much exactWhy differences between t3 App and tRPC starter app?I'm relatively new to using React and quickly adopted Typescript and NextJS having been inspired by How to invalidate routes/router in backend/webhook?Hello, I'm making an app using T3 that uses Stripe for subscription and payment. I have made purchaswhat are the best alternatives for axios ?Axios uses xhr and i searched a bit about the difference between fetch api and i found ou fetch api youtube videos blur effectanyone know any libs that do this type of effect where you have a blurry version of the video playinUi component nameanyone know where can i find a component that looks like this or at least can do the same job which Infer type of object values in arrayCan someone help me get this straight I want to infer all possible types of Value from T and get an use t3 to create a nextjs frontend and backend with nextjs to have login , create user with mysql?any example to use t3 to create my app using nextjs as frontend and option to login, add new user, e