Hocus
Hocus
DTDrizzle Team
Created by Hocus on 12/9/2023 in #help
`{ onDelete: "cascade" }` when the entity is in one-to-one relationship with multiple entities?
hey guys, whats the best way to use { onDelete: "cascade" } when the entity is in one-to-one relationship with multiple entities? I have a users, friendRequests and locations. users, friendRequests both have a location, and if a user or friendRequests gets deleted, I want to delete the corresponding location as well.
export const users = pgTable(
"users",
{
id: text("id")
.primaryKey()
locationId: text("locationId").references(() => locations.id),
},
);

export const usersRelations = relations(users, ({ one }) => ({
location: one(locations, {
fields: [users.locationId],
references: [locations.id],
}),
}));

export const friendRequests = pgTable(
"friendRequests",
{
sentById: text("sentById")
.references(() => users.id, { onDelete: "cascade" }),
receivedById: text("receivedById")
.references(() => users.id, { onDelete: "cascade" }),
locationId: text("locationId").references(() => locations.id),
},
(table) => ({
pk: primaryKey(table.sentById, table.receivedById),
sentBy: index("friendRequests_sentBy_idx").on(table.sentById),
receivedBy: index("friendRequests_receivedBy_idx").on(table.receivedById),
})
);

export const friendRequestsRelations = relations(friendRequests, ({ one }) => ({
sentBy: one(users, {
fields: [friendRequests.sentById],
references: [users.id],
relationName: RELATION_NAMES.FRIEND_REQ_SENT,
}),
receivedBy: one(users, {
fields: [friendRequests.receivedById],
references: [users.id],
relationName: RELATION_NAMES.FRIEND_REQ_RECEIVED,
}),
location: one(locations, {
fields: [friendRequests.locationId],
references: [locations.id],
}),
}));

export const locations = pgTable("location", {
id: text("id")
.primaryKey()
lat: doublePrecision("lat").notNull(),
lng: doublePrecision("lng").notNull(),
});
export const users = pgTable(
"users",
{
id: text("id")
.primaryKey()
locationId: text("locationId").references(() => locations.id),
},
);

export const usersRelations = relations(users, ({ one }) => ({
location: one(locations, {
fields: [users.locationId],
references: [locations.id],
}),
}));

export const friendRequests = pgTable(
"friendRequests",
{
sentById: text("sentById")
.references(() => users.id, { onDelete: "cascade" }),
receivedById: text("receivedById")
.references(() => users.id, { onDelete: "cascade" }),
locationId: text("locationId").references(() => locations.id),
},
(table) => ({
pk: primaryKey(table.sentById, table.receivedById),
sentBy: index("friendRequests_sentBy_idx").on(table.sentById),
receivedBy: index("friendRequests_receivedBy_idx").on(table.receivedById),
})
);

export const friendRequestsRelations = relations(friendRequests, ({ one }) => ({
sentBy: one(users, {
fields: [friendRequests.sentById],
references: [users.id],
relationName: RELATION_NAMES.FRIEND_REQ_SENT,
}),
receivedBy: one(users, {
fields: [friendRequests.receivedById],
references: [users.id],
relationName: RELATION_NAMES.FRIEND_REQ_RECEIVED,
}),
location: one(locations, {
fields: [friendRequests.locationId],
references: [locations.id],
}),
}));

export const locations = pgTable("location", {
id: text("id")
.primaryKey()
lat: doublePrecision("lat").notNull(),
lng: doublePrecision("lng").notNull(),
});
26 replies
DTDrizzle Team
Created by Hocus on 12/5/2023 in #help
Updated drizzle-kit -> Error: Cannot find module 'drizzle-orm/pg-core'
I updated drizzle from:
"drizzle-orm": "^0.29.1",
"drizzle-kit": "^0.20.6",
"drizzle-orm": "^0.29.1",
"drizzle-kit": "^0.20.6",
to
"drizzle-orm": "^0.28.5",
"drizzle-kit": "^0.19.3",
"drizzle-orm": "^0.28.5",
"drizzle-kit": "^0.19.3",
I updated my config to use the new defineConfig method and changed the connectionString to uri
export default defineConfig({
schema: "./src/server/db/schema.ts",
driver: "mysql2",
dbCredentials: {
uri: env.DATABASE_URL,
},
tablesFilter: ["bun-loconta_*"],
});
export default defineConfig({
schema: "./src/server/db/schema.ts",
driver: "mysql2",
dbCredentials: {
uri: env.DATABASE_URL,
},
tablesFilter: ["bun-loconta_*"],
});
but now I am getting this error when I try to run drizzle-kit: Error: Cannot find module 'drizzle-orm/pg-core' I dont use postgres, my driver is mysql2 using PlanetScale as DB provider. my package.json - I am using bun run incase it matters:
"scripts": {
"db:push": "dotenv drizzle-kit push:mysql",
"db:studio": "dotenv drizzle-kit studio",
},
"scripts": {
"db:push": "dotenv drizzle-kit push:mysql",
"db:studio": "dotenv drizzle-kit studio",
},
7 replies
DTDrizzle Team
Created by Hocus on 12/4/2023 in #help
Prepare statement throws type error for enums. How to get type safety for prepared values
No description
10 replies