Alex.dev
Alex.dev
Explore posts from servers
DTDrizzle Team
Created by Alex.dev on 11/12/2023 in #help
Set onDelete while using foreignKey function
No description
4 replies
DTDrizzle Team
Created by Alex.dev on 8/8/2023 in #help
Identifier is too long (should not exceed 63 characters)
Hello guys. I've got too long constraint id generated by drizzle kit in migration files. Is it safe to apply such migration? Maybe there is a way how I can set custom constraint id for some tables? Here is my schema
export const customerVehicleClaims = pgTable('customer_vehicle_claims', {
claimsid: text('claimsid').primaryKey().notNull(),
customervehicleid: integer('customervehicleid')
.notNull()
.references(() => customervehicle.customervehicleid),
createdAt: timestamp('created_at', { mode: 'string' }).defaultNow().notNull(),
updatedAt: timestamp('updated_at', { mode: 'string' }),
customerid: integer('customerid')
.notNull()
.references(() => customer.customerid),
vehicleid: integer('vehicleid')
.notNull()
.references(() => vehicle.vehicleid),
});
export const customerVehicleClaims = pgTable('customer_vehicle_claims', {
claimsid: text('claimsid').primaryKey().notNull(),
customervehicleid: integer('customervehicleid')
.notNull()
.references(() => customervehicle.customervehicleid),
createdAt: timestamp('created_at', { mode: 'string' }).defaultNow().notNull(),
updatedAt: timestamp('updated_at', { mode: 'string' }),
customerid: integer('customerid')
.notNull()
.references(() => customer.customerid),
vehicleid: integer('vehicleid')
.notNull()
.references(() => vehicle.vehicleid),
});
Here is the generated sql
DO
$$
BEGIN
ALTER TABLE "customer_vehicle_claims"
ADD CONSTRAINT "customer_vehicle_claims_customervehicleid_customervehicle_customervehicleid_fk" FOREIGN KEY ("customervehicleid") REFERENCES "customervehicle" ("customervehicleid") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END
$$;
--> statement-breakpoint
DO
$$
BEGIN
ALTER TABLE "customer_vehicle_claims"
ADD CONSTRAINT "customer_vehicle_claims_customervehicleid_customervehicle_customervehicleid_fk" FOREIGN KEY ("customervehicleid") REFERENCES "customervehicle" ("customervehicleid") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END
$$;
--> statement-breakpoint
12 replies
DTDrizzle Team
Created by Alex.dev on 6/18/2023 in #help
Delete using pg query in drizzle
Hello guys, is there any way to implement following query in drizzle?
DELETE FROM CustomerVehiclePart AS a
USING CustomerVehicle AS b,
Vehicle AS c
WHERE a.CustomerVehicleID = b.CustomerVehicleID
AND b.VehicleID = c.VehicleID
AND b.CustomerID = $1
AND c.SessionID = $2;
DELETE FROM CustomerVehiclePart AS a
USING CustomerVehicle AS b,
Vehicle AS c
WHERE a.CustomerVehicleID = b.CustomerVehicleID
AND b.VehicleID = c.VehicleID
AND b.CustomerID = $1
AND c.SessionID = $2;
4 replies
DTDrizzle Team
Created by Alex.dev on 5/8/2023 in #help
use constant in between operator
2 replies
DTDrizzle Team
Created by Alex.dev on 5/5/2023 in #help
Transaction rollback
Hello guys. Kind of dumb question but should I cover body of transaction in try/catch and call rollback explicitly? Here is an example. What will happen some query throws an error in transaction callback?
sessionId = await db.transaction(async (tx) => {
const [{insertedSessionId}] = await tx
.insert(session)
.values({
auctionid: values.auctionId,
auctiondate: values.auctionDate,
sessiondate: values.previewDate,
})
.returning({insertedSessionId: session.sessionid});
await tx.insert(customersession).values({
sessionid: insertedSessionId,
customerid: values.customerId,
isclone: false,
});
return insertedSessionId;
});
sessionId = await db.transaction(async (tx) => {
const [{insertedSessionId}] = await tx
.insert(session)
.values({
auctionid: values.auctionId,
auctiondate: values.auctionDate,
sessiondate: values.previewDate,
})
.returning({insertedSessionId: session.sessionid});
await tx.insert(customersession).values({
sessionid: insertedSessionId,
customerid: values.customerId,
isclone: false,
});
return insertedSessionId;
});
9 replies