© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•2y ago•
10 replies
jsingleton37

What's the best way to prevent duplicate entries?

I am building a voting app for college sports. A user can go and submit their Top 25 teams each week in a season.

My API route looks like
import * as Sentry from '@sentry/nextjs'

import { auth } from '@clerk/nextjs/server'
import { db } from '@/server/db'
import { votes } from '@/server/db/schema'

export async function POST(req: Request) {
  try {
    const body = await req.json()
    const user = auth()

    if (!user.userId) {
      return new Response('Unauthorized', { status: 401 })
    }

    const votesConverted = Object.entries(body)
      .map(([key, value]) => {
        const match = key.match(/rank_(\d+)/)
        if (!match) return null
        const rank = parseInt(match[1], 10)
        return { teamId: String(value), rank: rank } // Convert 'value' to a string
      })
      .map((vote) => ({
        userId: user.userId,
        week: 1,
        teamId: vote!.teamId,
        rank: vote!.rank,
      }))

    await db.insert(votes).values(votesConverted)

    return new Response('OK', { status: 200 })
  } catch (error) {
    Sentry.captureException(error)
    return Response.error()
  }
}
import * as Sentry from '@sentry/nextjs'

import { auth } from '@clerk/nextjs/server'
import { db } from '@/server/db'
import { votes } from '@/server/db/schema'

export async function POST(req: Request) {
  try {
    const body = await req.json()
    const user = auth()

    if (!user.userId) {
      return new Response('Unauthorized', { status: 401 })
    }

    const votesConverted = Object.entries(body)
      .map(([key, value]) => {
        const match = key.match(/rank_(\d+)/)
        if (!match) return null
        const rank = parseInt(match[1], 10)
        return { teamId: String(value), rank: rank } // Convert 'value' to a string
      })
      .map((vote) => ({
        userId: user.userId,
        week: 1,
        teamId: vote!.teamId,
        rank: vote!.rank,
      }))

    await db.insert(votes).values(votesConverted)

    return new Response('OK', { status: 200 })
  } catch (error) {
    Sentry.captureException(error)
    return Response.error()
  }
}


Right now I can just keep submitting the same form over and over again. Is there a quick and dirty way to just see if this user has submitted a vote for that week and year? Currently year is just autopopulated.
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

What's the best way to query my db?
Drizzle TeamDTDrizzle Team / help
2y ago
What's the best way to deploy migrations on production database?
Drizzle TeamDTDrizzle Team / help
3y ago
What is the best way to handle Query Errors?
Drizzle TeamDTDrizzle Team / help
9mo ago
What is the best way to order by exact matches
Drizzle TeamDTDrizzle Team / help
2y ago