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