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
Setup:
Turso / libsql, dialect is set to turso
Versions:
"@libsql/client": "^0.14.0"
"drizzle-orm": "^0.34.1"
"drizzle-kit": "^0.25.0"
Turso / libsql, dialect is set to turso
Versions:
"@libsql/client": "^0.14.0"
"drizzle-orm": "^0.34.1"
"drizzle-kit": "^0.25.0"
Hey I have created this table:
export const userAsyncTasks = createTable(
"userAsyncTask",
{
userId: text("userId").notNull(),
identifier: text("identifier").notNull(),
type: text("type", { enum: userAsyncTaskTypeEnum }).notNull(),
data: text("data", { mode: "json" }),
startedAt: int("startedAt", { mode: "timestamp" }).notNull(),
timeoutAt: int("timeoutAt", { mode: "timestamp" }),
},
(table) => ({
pk: primaryKey({ columns: [table.userId, table.type, table.identifier] }),
fk: foreignKey({
columns: [table.userId],
foreignColumns: [users.id],
})
.onDelete("cascade")
.onUpdate("cascade"),
}),
);
export const userAsyncTasks = createTable(
"userAsyncTask",
{
userId: text("userId").notNull(),
identifier: text("identifier").notNull(),
type: text("type", { enum: userAsyncTaskTypeEnum }).notNull(),
data: text("data", { mode: "json" }),
startedAt: int("startedAt", { mode: "timestamp" }).notNull(),
timeoutAt: int("timeoutAt", { mode: "timestamp" }),
},
(table) => ({
pk: primaryKey({ columns: [table.userId, table.type, table.identifier] }),
fk: foreignKey({
columns: [table.userId],
foreignColumns: [users.id],
})
.onDelete("cascade")
.onUpdate("cascade"),
}),
);
12 replies
DTDrizzle Team
Created by Dari on 9/20/2024 in #help
Using function in "and" instead of dynamic query
Hey i was wondering if this would work or if there would be any downsides
await db.query.myTable.findFirst({
where: and(
eq(myTable.identifier, params.identifier),
eq(myTable.userId, params.userId),
this.optionalAnd()
),
});

function optionalAnd() {
// some complex synchronous condition which will result in true or false
const doNothing = false; // simplified
if (doNothing) {
return undefined;
}

return and(
eq(myTable.someField, "someValue"),
eq(myTable.someOtherField, "someOtherValue")
);
}
await db.query.myTable.findFirst({
where: and(
eq(myTable.identifier, params.identifier),
eq(myTable.userId, params.userId),
this.optionalAnd()
),
});

function optionalAnd() {
// some complex synchronous condition which will result in true or false
const doNothing = false; // simplified
if (doNothing) {
return undefined;
}

return and(
eq(myTable.someField, "someValue"),
eq(myTable.someOtherField, "someOtherValue")
);
}
This is a simplified example. I prefered writing it like this instead of a dynamic query because i prefer its readability. But I'm not sure if I might run into issues with this. For now the code seems to work. Any suggestions? Im using turso as my db (sqlite aka libsql to be more precise). Thanks for your help 🙂
1 replies
DTDrizzle Team
Created by Dari on 7/3/2024 in #help
Drizzle Push to Turso fails on third attempt
If I use drizzle push on an empty turso db the first push works fine. (command: "pnpm db:push") After that if I run push again, it thinks that the schema doesn't match and it applies some changes. On the third db:push attempt it tries to do the changes again but this time it fails and it leaves the db in an "broken" state. (Because It has some _old tables left and the new ones are missing the values of the _old one.) I see two issues here. 1) Drizzle kit sees schema changes which are actually not there. 2) Drizzle kit is not pushing in a transaction and on error it is not getting rolled back Does anybody else have this issue / a solution? Here is a link to a minimal repo: https://github.com/Cuhadari-Deniz/testing-drizzle-turso drizzle-orm: 0.30.10 drizzle-kit: 0.21.4 @libsql/client: 0.6.0
3 replies