Query transaction question

A question about queries/transactions:

If a document exists, then I want to create a different document and also delete document 1.

The problem is that deleting the document seems to trigger the abort call.

let inv = Invitation
    .by_token(${token})
    .first()

if (inv == null) {
    abort("Bad invitation token")
}

if (inv!.status != "pending") {
    abort("That invitation has been used already.")
}

let user = User.create({
    email: inv!.email,
    dbName: inv!.dbName,
    password: ${hashedPassword}
}) { id, email, dbName }

inv!.delete()

user

This query throws because the abort("Bad invitation token") is called, even though the invitation exists. I’m guessing that the inv.delete at the end is causing that, which is surprising to me.

I see you asked this in Discord, too. If you reach out for help in both places, please be sure to note that so that folks are not duplicating efforts. https://discord.com/channels/826528159389581342/826528271998910464/1306073799300481114

Like Wallslide suggested, maybe you are running the same query multiple times? The delete at the end of the query cannot affect the if expression above it, which means that the token must not be found when the abort happens.