RangeError: Maximum call stack size exceeded on update and delete for small number of rows (10s)

I have the following two calls that return the same RangeError:
await db
.update(schema.cards)
.set({ redeemable: true })
.where(eq(schema.cards.contractAddress, vaultAddress))
.returning({ address: vaultAddress })
.catch((error) => {
console.error(error);
error(500);
});

await db
.delete(schema.cards)
.where(
and(
eq(schema.accounts.address, address),
eq(schema.cards.contractAddress, vaultAddress),
),
)
.returning({ address })
.catch((error) => {
console.error(error);
error(500);
});
await db
.update(schema.cards)
.set({ redeemable: true })
.where(eq(schema.cards.contractAddress, vaultAddress))
.returning({ address: vaultAddress })
.catch((error) => {
console.error(error);
error(500);
});

await db
.delete(schema.cards)
.where(
and(
eq(schema.accounts.address, address),
eq(schema.cards.contractAddress, vaultAddress),
),
)
.returning({ address })
.catch((error) => {
console.error(error);
error(500);
});
The number of rows in question is around 10. I am successfully using .where with update in another query.
30 Replies
rphlmr ⚡
rphlmr ⚡4mo ago
Is it exactly how your delete is written? .returning({ address }) is invalid, you need to .returning({ address: schema.accounts.address }) or an other column you are assigning address to .returning looks the same for update maybe it is not clear but returnin is {aName: theTableColumn}
shot
shot4mo ago
thank you! that solved the first and led to another issue for the latter (deletion). so I get the following: PostgresError: missing FROM-clause entry for table "accounts" Which I suppose makes sense; there is a one-to-many relation from accounts -> cards, I would like to delete all cards that belong to the specific account based on an address of that account, but the reference in cards is to the accounts id
rphlmr ⚡
rphlmr ⚡4mo ago
oh you want a subquery in your where
shot
shot4mo ago
accounts.address is unique, would one way of addressing this being making the relation reference the address in the card relation? Oh I like that option much more. I will have a look at the docs for that ❤️
rphlmr ⚡
rphlmr ⚡4mo ago
Relations are only for select. https://orm.drizzle.team/docs/operators#exists I am thinking about exists but it depends on the query you need
I would like to delete all cards that belong to the specific account based on an address of that account
if there is a way to match a card and account it could be possible
rphlmr ⚡
rphlmr ⚡4mo ago
you can try to reproduce a light version of your schema and share it with https://drizzle.run
Drizzle Run
Drizzle Run
shot
shot4mo ago
woops, forgot a field, changes pushed the playground is 🤌 btw and maybe to be a bit clearer with the query I am after: Delete all cards - that match a given contractAddress and - that reference the account given by the account.address. So cards that match the contractAddress but reference another account are not deleted
rphlmr ⚡
rphlmr ⚡4mo ago
Ok, I'm forking it and come back in few minutes. If you try on your own we could compare :p
shot
shot4mo ago
I will try 🙂
rphlmr ⚡
rphlmr ⚡4mo ago
can a cards have no accountId ?
shot
shot4mo ago
strictly, yes, because there is no notNull. This isn't the case in my application however
rphlmr ⚡
rphlmr ⚡4mo ago
ok because we could add a reference from accountId to accounts.id but I was just asking :p
shot
shot4mo ago
for this POC I am making for tomorrow I won't include the notNull but it should be there I suppose as all cards should be related to an account wait, isn't that what there is already?
rphlmr ⚡
rphlmr ⚡4mo ago
Relations are "runtime" link between tables but not "real" link like references (foreign key)
shot
shot4mo ago
ah so you're saying I could add a database-level relation with foreign keys?
rphlmr ⚡
rphlmr ⚡4mo ago
yes, only if you need to prevent errors
shot
shot4mo ago
If that is my only option I will have to but I'm trying to avoid changes to the schema if possible atm 😉
rphlmr ⚡
rphlmr ⚡4mo ago
With a ref, you have the guarantee that it will be an existing accountId
shot
shot4mo ago
Right, I see
rphlmr ⚡
rphlmr ⚡4mo ago
ok
shot
shot4mo ago
hmm, with the following I delete all cards but for every user. Will keep trying.
const query = db.select().from(accounts).where(eq(accounts.address, "0xdeadbeef"))

const result = await db
.delete(cards)
.where(
and(
exists(query),
eq(cards.contractAddress, "0xsomething"),
),
)
const query = db.select().from(accounts).where(eq(accounts.address, "0xdeadbeef"))

const result = await db
.delete(cards)
.where(
and(
exists(query),
eq(cards.contractAddress, "0xsomething"),
),
)
Which I guess makes sense, as exists(query) I expect to always be true with how I have defined it.
rphlmr ⚡
rphlmr ⚡4mo ago
I did the same query
rphlmr ⚡
rphlmr ⚡4mo ago
It seems to delete the expected cards
shot
shot4mo ago
If you add the card value, in your playground:
{
id: 4,
accountId: 2,
contractAddress: "contract-aa",
},
{
id: 4,
accountId: 2,
contractAddress: "contract-aa",
},
This will also get deleted though no? i.e. it is deleting cards that match the contractAddress but for every user.
rphlmr ⚡
rphlmr ⚡4mo ago
ah yes 🤦‍♂️ I have updated
const checkQuery = db
.select()
.from(accounts)
.where(and(eq(accounts.address, address), eq(cards.accountId, accounts.id)));
const checkQuery = db
.select()
.from(accounts)
.where(and(eq(accounts.address, address), eq(cards.accountId, accounts.id)));
now the check is also checking the accountId
shot
shot4mo ago
ahhh this makes good sense. Thanks for all the help! its greatly appreciated Curious, how big is the team working on drizzle?
rphlmr ⚡
rphlmr ⚡4mo ago
I think they are 4 developers (I am not a core member, just an early adopter contributing lol)
shot
shot4mo ago
awesome, well, thanks again ❤️
Want results from more Discord servers?
Add your server