Dari
Dari
Explore posts from servers
DTDrizzle Team
Created by Dari on 10/11/2024 in #help
Resolved: Need help with my multi column primary and foreign key table
shall i open an issue?
12 replies
DTDrizzle Team
Created by Dari on 10/11/2024 in #help
Resolved: Need help with my multi column primary and foreign key table
@Andrew Sherman is this intended behaviour or a bug?
12 replies
DTDrizzle Team
Created by Dari on 10/11/2024 in #help
Resolved: Need help with my multi column primary and foreign key table
Found the issue.
pk: primaryKey({ columns: [table.userId, table.type, table.identifier] }),
pk: primaryKey({ columns: [table.userId, table.type, table.identifier] }),
The columns need to be in the correct order. So it worked as soon as I changed it to:
pk: primaryKey({ columns: [table.userId, table.identifier, table.type] }),
pk: primaryKey({ columns: [table.userId, table.identifier, table.type] }),
12 replies
DTDrizzle Team
Created by Dari on 10/11/2024 in #help
Resolved: Need help with my multi column primary and foreign key table
The first db push works fine. On a second call without any changes it still tries to "alter" the table. First db:push:
CREATE TABLE `userAsyncTask` (
`userId` text NOT NULL,
`identifier` text NOT NULL,
`type` text NOT NULL,
`data` text,
`startedAt` integer NOT NULL,
`timeoutAt` integer,
PRIMARY KEY(`userId`, `type`, `identifier`),
FOREIGN KEY (`userId`) REFERENCES `user`(`id`) ON UPDATE cascade ON DELETE cascade
);
CREATE TABLE `userAsyncTask` (
`userId` text NOT NULL,
`identifier` text NOT NULL,
`type` text NOT NULL,
`data` text,
`startedAt` integer NOT NULL,
`timeoutAt` integer,
PRIMARY KEY(`userId`, `type`, `identifier`),
FOREIGN KEY (`userId`) REFERENCES `user`(`id`) ON UPDATE cascade ON DELETE cascade
);
Second db:push:
CREATE TABLE `__new_userAsyncTask` (
`userId` text NOT NULL,
`identifier` text NOT NULL,
`type` text NOT NULL,
`data` text,
`startedAt` integer NOT NULL,
`timeoutAt` integer,
PRIMARY KEY(`userId`, `type`, `identifier`),
FOREIGN KEY (`userId`) REFERENCES `user`(`id`) ON UPDATE cascade ON DELETE cascade
);

INSERT INTO `__new_userAsyncTask`("userId", "identifier", "type", "data", "startedAt", "timeoutAt") SELECT "userId", "identifier", "type", "data", "startedAt", "timeoutAt" FROM `userAsyncTask`;
DROP TABLE `userAsyncTask`;
ALTER TABLE `__new_userAsyncTask` RENAME TO `userAsyncTask`;
CREATE TABLE `__new_userAsyncTask` (
`userId` text NOT NULL,
`identifier` text NOT NULL,
`type` text NOT NULL,
`data` text,
`startedAt` integer NOT NULL,
`timeoutAt` integer,
PRIMARY KEY(`userId`, `type`, `identifier`),
FOREIGN KEY (`userId`) REFERENCES `user`(`id`) ON UPDATE cascade ON DELETE cascade
);

INSERT INTO `__new_userAsyncTask`("userId", "identifier", "type", "data", "startedAt", "timeoutAt") SELECT "userId", "identifier", "type", "data", "startedAt", "timeoutAt" FROM `userAsyncTask`;
DROP TABLE `userAsyncTask`;
ALTER TABLE `__new_userAsyncTask` RENAME TO `userAsyncTask`;
Any suggestions? I've tried defining the foreign key with the foreignKey function and directly in the schema field with .references(). Same result
12 replies
DTDrizzle Team
Created by brettlyc on 9/19/2024 in #help
transaction is not defined
btw drizzle rollsback automatically if any error is thrown within the transaction function. So you wouldn need to try catch and if an error occurs it should rollback as expected
11 replies
DTDrizzle Team
Created by brettlyc on 9/19/2024 in #help
transaction is not defined
const createNewSchedules = async (scheduleData, store) => {
const startTime = getDate(scheduleData.start_time)
const endTime = getDate(scheduleData.end_time)
const daysToSchedule = getDaysBetween(scheduleData.dates)

return await db.transaction(async (tx) => {
await Promise.all(daysToSchedule.map(async (date) => {
scheduleData.date = new Date(date)
scheduleData.start_time = getDate(date).set('hour', startTime.get('hour')).set('minute', startTime.get('minute')).toDate()
scheduleData.end_time = getDate(date).set('hour', endTime.get('hour')).set('minute', endTime.get('minute')).toDate()

const validatedSchedule = await TechnicianSchedule.parseAsync(scheduleData)
if (validatedSchedule.errors) throw validatedSchedule.errors

try {
await verifyTechSchedules(validatedSchedule)

await tx.insert(technicianSchedules).values({
...validatedSchedule,
store,
schedule_created: new Date()
})
} catch (err) {
tx.rollback()
}
}))
})
}
const createNewSchedules = async (scheduleData, store) => {
const startTime = getDate(scheduleData.start_time)
const endTime = getDate(scheduleData.end_time)
const daysToSchedule = getDaysBetween(scheduleData.dates)

return await db.transaction(async (tx) => {
await Promise.all(daysToSchedule.map(async (date) => {
scheduleData.date = new Date(date)
scheduleData.start_time = getDate(date).set('hour', startTime.get('hour')).set('minute', startTime.get('minute')).toDate()
scheduleData.end_time = getDate(date).set('hour', endTime.get('hour')).set('minute', endTime.get('minute')).toDate()

const validatedSchedule = await TechnicianSchedule.parseAsync(scheduleData)
if (validatedSchedule.errors) throw validatedSchedule.errors

try {
await verifyTechSchedules(validatedSchedule)

await tx.insert(technicianSchedules).values({
...validatedSchedule,
store,
schedule_created: new Date()
})
} catch (err) {
tx.rollback()
}
}))
})
}
Try this, I think it should fix it.
11 replies
DTDrizzle Team
Created by brettlyc on 9/19/2024 in #help
transaction is not defined
transaction does not return the transaction. Instead it provides the transaction within the lambda function. So it definetly needs a function which executes within the transaction. Something like this:
await db.transaction((trx) => {
// use and pass trx where needed
trx.insert(helloTable).values({ name: "hello" });
await myRandomFunction(trx, randomParameters);
});
await db.transaction((trx) => {
// use and pass trx where needed
trx.insert(helloTable).values({ name: "hello" });
await myRandomFunction(trx, randomParameters);
});
Basically you need to wrap the code with
db.transaction((trx) => {
//...yourCode
});
db.transaction((trx) => {
//...yourCode
});
Hope that helps 🙂
11 replies
DTDrizzle Team
Created by Dari on 7/3/2024 in #help
Drizzle Push to Turso fails on third attempt
Wow that solved it. Thank you very much!
3 replies