N
Neon2mo ago
rival-black

Need help in connecting and referencing table in `neon_auth` schema

Recently started using stack auth with neon. The data for users is coming in users_sync under neon_auth schema. I am having a problem while connecting to this schema. My Drizzle config defaults to public schema and creates a new table named users_sync. Here is my table structure in Drizzle,
import {
pgTable,
text,
timestamp,
pgEnum,
boolean,
integer,
date,
uuid,
} from "drizzle-orm/pg-core";

export const userRoleEnum = pgEnum("user_role", ["student", "mentor", "admin"]);
export const opportunityTypeEnum = pgEnum("opportunity_type", [
"hackathon",
"grant application",
"competition",
"ideathon",
]);

export const comments = pgTable("comments", {
id: uuid("id").primaryKey().defaultRandom(),
content: text("content").notNull(),
createdAt: timestamp("created_at").defaultNow(),
updatedAt: timestamp("updated_at").defaultNow(),
userId: text("user_id").references(() => users_sync.id),
opportunityId: uuid("opportunity_id").references(() => opportunities.id),
});

export const opportunities = pgTable("opportunities", {
id: uuid("id").primaryKey().defaultRandom(),
type: opportunityTypeEnum("type").array().default([]),
title: text("title").notNull(),
description: text("description").notNull(),
url: text("url").notNull(),
image: text("image"),
tags: text("tags").array().default([]),
location: text("location"),
organiserInfo: text("organiser_info"),
startDate: date("start_date"),
endDate: date("end_date"),
comments: text("comments").array().default([]),
isFlagged: boolean("is_flagged").default(false),
createdAt: timestamp("created_at").defaultNow(),
updatedAt: timestamp("updated_at").defaultNow(),
isVerified: boolean("is_verified").default(false),
isActive: boolean("is_active").default(true),
createdByUser: text("created_by_user").references(() => users_sync.id),
});
import {
pgTable,
text,
timestamp,
pgEnum,
boolean,
integer,
date,
uuid,
} from "drizzle-orm/pg-core";

export const userRoleEnum = pgEnum("user_role", ["student", "mentor", "admin"]);
export const opportunityTypeEnum = pgEnum("opportunity_type", [
"hackathon",
"grant application",
"competition",
"ideathon",
]);

export const comments = pgTable("comments", {
id: uuid("id").primaryKey().defaultRandom(),
content: text("content").notNull(),
createdAt: timestamp("created_at").defaultNow(),
updatedAt: timestamp("updated_at").defaultNow(),
userId: text("user_id").references(() => users_sync.id),
opportunityId: uuid("opportunity_id").references(() => opportunities.id),
});

export const opportunities = pgTable("opportunities", {
id: uuid("id").primaryKey().defaultRandom(),
type: opportunityTypeEnum("type").array().default([]),
title: text("title").notNull(),
description: text("description").notNull(),
url: text("url").notNull(),
image: text("image"),
tags: text("tags").array().default([]),
location: text("location"),
organiserInfo: text("organiser_info"),
startDate: date("start_date"),
endDate: date("end_date"),
comments: text("comments").array().default([]),
isFlagged: boolean("is_flagged").default(false),
createdAt: timestamp("created_at").defaultNow(),
updatedAt: timestamp("updated_at").defaultNow(),
isVerified: boolean("is_verified").default(false),
isActive: boolean("is_active").default(true),
createdByUser: text("created_by_user").references(() => users_sync.id),
});
I have tried to write the table as neon_auth.users_sync but it doesn't work. Any idea how I can connect and reference users?
2 Replies
unwilling-turquoise
unwilling-turquoise2mo ago
Hi @Khushal Sharma, in Drizzle you can switch schemas with the pgSchema helper but in this case you can also use the helper function provided by drizzle-orm/neon. It exports a helper schema for this called usersSync that you can use.
import { usersSync } from "drizzle-orm/neon"
import { usersSync } from "drizzle-orm/neon"
You can find the implementation here: https://github.com/drizzle-team/drizzle-orm/blob/main/drizzle-orm/src/neon/neon-identity.ts Note that if you re-export usersSync drizzle-orm generate will add the table create instructions to your schema changes. You'll need to comment that out because the table already exists. 🙂
GitHub
drizzle-orm/drizzle-orm/src/neon/neon-identity.ts at main · drizzl...
Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅 - drizzle-team/drizzle-orm
rival-black
rival-blackOP2mo ago
Thanks @Andre Landgraf I will take a look 🙂

Did you find this page helpful?