The cultured one
The cultured one
DTDrizzle Team
Created by The cultured one on 1/13/2024 in #help
Deletion causes error because it violates foreign key constraint when it shouldnt
Here is the relevant schema: the "one":
export const aditionalService = pgTable("aditionalService", {
id: prefixedUlid(aditionalServicePrefix).$type<aditionalServiceID>(),

name: varchar("name").unique().notNull(),
price: integer("price").notNull(),

...defaultTimestamps,
});

export const aditionalServiceRelations = relations(
aditionalService,
({ many }) => ({
tasks: many(task,),
}),
);
export const aditionalService = pgTable("aditionalService", {
id: prefixedUlid(aditionalServicePrefix).$type<aditionalServiceID>(),

name: varchar("name").unique().notNull(),
price: integer("price").notNull(),

...defaultTimestamps,
});

export const aditionalServiceRelations = relations(
aditionalService,
({ many }) => ({
tasks: many(task,),
}),
);
the "many":
export const task = pgTable("task", {
id: prefixedUlid(taskPrefix).$type<taskID>(),

name: varchar("name").notNull(),
isExternal: boolean("isExternal").default(false).notNull(),

aditionalServiceId: varchar("aditionalServiceId")
.$type<aditionalServiceID>()
.notNull()
.references(() => aditionalService.id, { onDelete: 'cascade' }),

...defaultTimestamps,
});

export const taskRelations = relations(task, ({ one }) => ({
aditionalService: one(aditionalService, {
fields: [task.aditionalServiceId],
references: [aditionalService.id],

}),
}));
export const task = pgTable("task", {
id: prefixedUlid(taskPrefix).$type<taskID>(),

name: varchar("name").notNull(),
isExternal: boolean("isExternal").default(false).notNull(),

aditionalServiceId: varchar("aditionalServiceId")
.$type<aditionalServiceID>()
.notNull()
.references(() => aditionalService.id, { onDelete: 'cascade' }),

...defaultTimestamps,
});

export const taskRelations = relations(task, ({ one }) => ({
aditionalService: one(aditionalService, {
fields: [task.aditionalServiceId],
references: [aditionalService.id],

}),
}));
When I attempt to delete an additonalService in theory the tasks should cascade from what I've been researching about onDelete actions and foreign key constraints. The example in the documentation seems for onDelete seems to also be a one to many so I am not sure what I'm doing wrong. https://orm.drizzle.team/docs/rqb#foreign-key-actions:~:text=In%20the%20following%20example%2C%20adding%20onDelete%3A%20%27cascade%27%20to%20the%20author%20field%20on%20the%20posts%20schema%20means%20that%20deleting%20the%20user%20will%20also%20delete%20all%20related%20Post%20records. Here is the error :
error: update or delete on table "aditionalService" violates foreign key constraint "task_aditionalServiceId_aditionalService_id_fk" on table "task"
error: update or delete on table "aditionalService" violates foreign key constraint "task_aditionalServiceId_aditionalService_id_fk" on table "task"
3 replies
DTDrizzle Team
Created by The cultured one on 1/9/2024 in #help
Populate does not work for a certain case ('populate = with')
Basically I have this endpoint that creates a booking and I cannot figure out why taskInstances does not get populated. Also the choice of the word 'with' makes searching for this particular problem on the internet very hard. If I had the task of coming up with a name I would probably just stick with the classic 'populate'. I have provided you with a slice of my repo modified for the easy reproduction of this exact case: https://github.com/XenoWad01/drizzle-with-repro Just ctrl + shift + f for Now why in gods name does this return [] when in studio there clearely are task-instances????? to see where the populate should happen and does not. I have even checked studio and the taskInstances are for sure there so this might just be an issue with populate or maybe on my part, not sure. You can find the schema inside packages/bookd-api/db/schema/ and everything else related to drizzle's setup inside packages/bookd-api/db. Steps to set up repo (I am assuming you have docker compose and Docker desktop set up for this): 1) go inside packages/bookd-api/ and make a .env file and copy over everything from the .env.example inside it. 2) docker compose up -d 3) yarn run install at the project root 4) yarn run dev 5) You can now navigate to http://localhost:3000/ (or just refresh the page) to trigger the reproduction case. The message mentioned above with Now why in gods name does this return [] when in studio there clearely are task-instances????? -> should be logged to the terminal with the value of taskInstances being []. You can repeat this endlessly it just creates everything needed for this step to happen on each page refresh (with randomized data for everything that for example needs to be unique). Sorry if the reproduction repo is a bit large, I figured if there's something wrong with my setup It would provide contextual information that might be needed. Thank you for your time!
9 replies
DTDrizzle Team
Created by The cultured one on 12/13/2023 in #help
Wierd error when trying to run drizzle studio
I want to start with saying sorry if the problem is obvious but I cannot seem to figure it out. I am getting a wierd error when I try to run drizzle studio.
drizzle-kit: v0.20.6
drizzle-orm: v0.29.1
drizzle-kit: v0.20.6
drizzle-orm: v0.29.1
here are my package.json scripts that are relevant:
"studio": "drizzle-kit studio --port 5666 --verbose --config ./db/drizzle.config.ts "
"studio": "drizzle-kit studio --port 5666 --verbose --config ./db/drizzle.config.ts "
drizzle.config.ts is:
import type { Config } from "drizzle-kit";
import * as dotenv from "dotenv";
dotenv.config();
import { dbUrl } from "./config";

export default {
schema: "./db/schema/*",
out: "./drizzle",
driver: 'pg',
dbCredentials: {
connectionString: dbUrl,
}
} satisfies Config;
import type { Config } from "drizzle-kit";
import * as dotenv from "dotenv";
dotenv.config();
import { dbUrl } from "./config";

export default {
schema: "./db/schema/*",
out: "./drizzle",
driver: 'pg',
dbCredentials: {
connectionString: dbUrl,
}
} satisfies Config;
9 replies