quitelistener
quitelistener
Explore posts from servers
DTDrizzle Team
Created by quitelistener on 10/10/2023 in #help
Pg: Inserting new data with auto increment column
I am getting error while inserting data to postgres from nextjs api. Error: detail: 'Key (id)=(21) already exists.', I have table with three columns and the API is submitting values as name and status. I am guessing postgres would take care of auto increment. what is the issue here ?
id: serial('id').primaryKey(),
name: varchar('name', { length: 256 }).notNull(),
status: varchar('status', { length: 100 }).notNull().default('DRAFT')
id: serial('id').primaryKey(),
name: varchar('name', { length: 256 }).notNull(),
status: varchar('status', { length: 100 }).notNull().default('DRAFT')
API
const res = await db.insert(dashboards)
.values({ ...json, status: 'DRAFT' }).returning({ id: dashboards.id })
return NextResponse.json(res, { status: 200 })
const res = await db.insert(dashboards)
.values({ ...json, status: 'DRAFT' }).returning({ id: dashboards.id })
return NextResponse.json(res, { status: 200 })
5 replies
DTDrizzle Team
Created by quitelistener on 6/16/2023 in #help
using postgres-js for connection
I am trying to setup drizzle with postgres-js package ( as document it be fastest). Referring to this doc: https://orm.drizzle.team/docs/installation-and-db-connection/postgresql/postgresjs How do i define schema using postgres-js ?? I tried import for table to my schema file and typescript shows error: Module '"drizzle-orm/postgres-js"' has no exported member ... How can i import table and other types ?? shoud I use 'drizzle-orm/pg-core' package for schema and 'drizzle-orm/postgres-js' for my index.ts ?? schema.ts :
import { integer, pgEnum, pgTable, serial, uniqueIndex, varchar,boolean,text,timestamp } from 'drizzle-orm/postgres-js';
import { integer, pgEnum, pgTable, serial, uniqueIndex, varchar,boolean,text,timestamp } from 'drizzle-orm/postgres-js';
index .ts :
import { drizzle, PostgresJsDatabase } from 'drizzle-orm/postgres-js';
import postgres from 'postgres';

const connectionString = process.env.DATABASE_URL || "";

const queryClient = postgres(connectionString, { max: 10 });
const db: PostgresJsDatabase = drizzle(queryClient);
import { drizzle, PostgresJsDatabase } from 'drizzle-orm/postgres-js';
import postgres from 'postgres';

const connectionString = process.env.DATABASE_URL || "";

const queryClient = postgres(connectionString, { max: 10 });
const db: PostgresJsDatabase = drizzle(queryClient);
3 replies