helloworld
helloworld
Explore posts from servers
DTDrizzle Team
Created by helloworld on 8/29/2023 in #help
Each element of array is a foreign key
Hello, I'm trying to figure out how to create a postgres schema that will hold an array of ids property, and each element of that array is a foreign key. Is this possible with Drizzle? Here's my implementation:
export const rooms = pgTable(
"room",
{
id: text("id").notNull().primaryKey(),
creatorId: text("creator_id")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
// ...
participantsIds: text("participants_ids")
.array()
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
// ...
}
);

export const usersRelations = relations(users, ({ one, many }) => ({
enteredRoom: one(rooms, {
fields: [users.id],
references: [rooms.participantsIds],
}),
createdRoom: one(rooms, {
fields: [users.id],
references: [rooms.creatorId],
}),
}));

export const roomsRelations = relations(rooms, ({ one, many }) => ({
participants: many(users),
creator: one(users, {
fields: [rooms.creatorId],
references: [users.id],
}),
}));
export const rooms = pgTable(
"room",
{
id: text("id").notNull().primaryKey(),
creatorId: text("creator_id")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
// ...
participantsIds: text("participants_ids")
.array()
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
// ...
}
);

export const usersRelations = relations(users, ({ one, many }) => ({
enteredRoom: one(rooms, {
fields: [users.id],
references: [rooms.participantsIds],
}),
createdRoom: one(rooms, {
fields: [users.id],
references: [rooms.creatorId],
}),
}));

export const roomsRelations = relations(rooms, ({ one, many }) => ({
participants: many(users),
creator: one(users, {
fields: [rooms.creatorId],
references: [users.id],
}),
}));
But I'm getting an error:
PostgresError: foreign key constraint "room_participants_ids_user_id_fk" cannot be implemented
PostgresError: foreign key constraint "room_participants_ids_user_id_fk" cannot be implemented
5 replies
TTCTheo's Typesafe Cult
Created by helloworld on 8/26/2023 in #questions
T3 ENV fails when trying to generate migrations
When i try to generate drizzle migrations through drizzle-kit generate:pg it fails with message:
Invalid environment variables: {
DATABASE_URL: [ 'Required' ],
NEXTAUTH_URL: [ 'Required' ],
GOOGLE_CLIENT_ID: [ 'Required' ],
GOOGLE_CLIENT_SECRET: [ 'Required' ],
NEXTAUTH_SECRET: [ 'Required' ]
}

node_modules\.pnpm\@t3-oss+env-nextjs@0.6.1_typescript@5.2.2_zod@3.22.2\node_modules\@t3-oss\core\index.ts:207
!prop.startsWith(opts.clientPrefix) &&
^


Error: Invalid environment variables
Invalid environment variables: {
DATABASE_URL: [ 'Required' ],
NEXTAUTH_URL: [ 'Required' ],
GOOGLE_CLIENT_ID: [ 'Required' ],
GOOGLE_CLIENT_SECRET: [ 'Required' ],
NEXTAUTH_SECRET: [ 'Required' ]
}

node_modules\.pnpm\@t3-oss+env-nextjs@0.6.1_typescript@5.2.2_zod@3.22.2\node_modules\@t3-oss\core\index.ts:207
!prop.startsWith(opts.clientPrefix) &&
^


Error: Invalid environment variables
I have my environment variables inside .env file, and app doesn't break. There's only problem when generating migrations. Has somebody encountered similar problem?
1 replies
TTCTheo's Typesafe Cult
Created by helloworld on 7/8/2023 in #questions
How to pass typesafe react-hook-form form object
I'm using react-hook-form to handle update user profile form.
const form = useForm<FormData>({
resolver: zodResolver(updateProfileSchema),
defaultValues: {
firstName: undefined,
lastName: undefined,
email: undefined,
oldPassword: undefined,
newPassword: undefined,
imageUrl: undefined,
},
mode: 'onSubmit',
});
const form = useForm<FormData>({
resolver: zodResolver(updateProfileSchema),
defaultValues: {
firstName: undefined,
lastName: undefined,
email: undefined,
oldPassword: undefined,
newPassword: undefined,
imageUrl: undefined,
},
mode: 'onSubmit',
});
I'm also using uploadthing for image upload (updating user profile pic), so I've created a custom upload button component and I'm passing to it a form, so I could setValue manually when upload is complete.
<UploadButton
form={form}
onUploadComplete={(url: string) => setProfileImageUrl(url)}
/>
<UploadButton
form={form}
onUploadComplete={(url: string) => setProfileImageUrl(url)}
/>
Inside UploadButtonProps I've declared form prop as a UseFormReturn type like this
interface UploadButtonProps {
form: UseFormReturn;
onUploadComplete: (url: string) => void;
}
interface UploadButtonProps {
form: UseFormReturn;
onUploadComplete: (url: string) => void;
}
So, the problem is now when I'm trying to use form to setValue I do not have typesafety.
form.setValue('imageUrl', imageUrl);
form.setValue('imageUrl', imageUrl);
How to achieve typesafety? I'm also using zod for form schema.
2 replies
TTCTheo's Typesafe Cult
Created by helloworld on 7/5/2023 in #questions
Clerk: Is it possible to create custom metadata on signup using Clerk?
I need to create a custom metadata "credits" on each new user object, is it possible to achieve that with Clerk? One of my ideas was to call the api route after sign up which will update user object using clerkClient.
await signUp.authenticateWithRedirect({
strategy: provider,
redirectUrl: '/dashboard',
redirectUrlComplete: '/dashboard',
});

await fetch('...');
await signUp.authenticateWithRedirect({
strategy: provider,
redirectUrl: '/dashboard',
redirectUrlComplete: '/dashboard',
});

await fetch('...');
But fetch never get called because authenticateWithRedirect automatically redirects to the /dashboard as soon as sign up happens. Any ideas, how to achieve this? I need to create public metadata, so unsafeMetadata is not a solution.
4 replies