transations

Greetings! I am trying to perform 2 inserts to my SQLite db, but I want that if one of the 2 fails, it will revert any changes made to it. I then decided to use a transaction. My goal is to achieve that when inserting an account, a user is then created and associated with it. But I get an error when executing this function.
No description
No description
16 Replies
DoggeSlapper
DoggeSlapper•2mo ago
It's looks like the db cant obtain a lock. Anything else runing at same time? try changing transaction mode to see if u cant see what's happening there
edarcode
edarcodeOP•2mo ago
nothing, it's the only transaction
DoggeSlapper
DoggeSlapper•2mo ago
await db.transaction(
async (tx) => {
await tx.update(accounts).set({ balance: sql`${accounts.balance} - 100.00` }).where(eq(users.name, "Dan"));
await tx.update(accounts).set({ balance: sql`${accounts.balance} + 100.00` }).where(eq(users.name, "Andrew"));
}, {
behavior: "deferred",
}
);
interface SQLiteTransactionConfig {
behavior?: 'deferred' | 'immediate' | 'exclusive';
}
await db.transaction(
async (tx) => {
await tx.update(accounts).set({ balance: sql`${accounts.balance} - 100.00` }).where(eq(users.name, "Dan"));
await tx.update(accounts).set({ balance: sql`${accounts.balance} + 100.00` }).where(eq(users.name, "Andrew"));
}, {
behavior: "deferred",
}
);
interface SQLiteTransactionConfig {
behavior?: 'deferred' | 'immediate' | 'exclusive';
}
try diferent behavior then to see if that helps debug the issue
edarcode
edarcodeOP•2mo ago
ok. let me commit and try.
edarcode
edarcodeOP•2mo ago
@DoggeSlapper equal err
No description
edarcode
edarcodeOP•2mo ago
No description
DoggeSlapper
DoggeSlapper•2mo ago
what if u had some console log? get some info there? I'm not using sqlite myself so cant really help much sorry that were my 2 cents 🙂
edarcode
edarcodeOP•2mo ago
after line 5
No description
edarcode
edarcodeOP•2mo ago
the info arrive good, but explot when try transation
DoggeSlapper
DoggeSlapper•2mo ago
I suspect it's lock on first insert
edarcode
edarcodeOP•2mo ago
I really don't understand how it works, but the await is supposed to wait before doing the second insert, right? I even think that it shouldn't matter if I block it because I actually want to wait for the first insert to finish so that it does the second one, because I depend on knowing the id.
DoggeSlapper
DoggeSlapper•2mo ago
wait I saw the problem u using db not tx so that's the reason why u blocking it
edarcode
edarcodeOP•2mo ago
xD
DoggeSlapper
DoggeSlapper•2mo ago
funny enough I paste doc but didnt notice the error hehehe
edarcode
edarcodeOP•2mo ago
Obviously I'm an idiot xD. 2h trying to solve it and it was 2 letters, good GPT chat doesn't even work to warn us what it was xd and they'll even take away our jobs, dammit thanks bro @DoggeSlapper
DoggeSlapper
DoggeSlapper•2mo ago
hahahah
Want results from more Discord servers?
Add your server