db.insert doesn't immediately insert the values into the DB (Neon)
I am trying to insert some users and some organizations and then define membership relation between both of these. I have set a foreign key relation in the memberships table that references the id of orgs. Now, when I try to insert values in all three tables one after other I am not able to do it as it says the org id doesn't exist.
When I do db.select statement just after db.insert, it results in an empty array. Is this the expected behavior? Why isn't db.insert promise resolving only after the data has been inserted? Thanks.
27 Replies
I am trying to use the org ids after putting a
.returning()
on the db.insert(orgs)
statement.
This is ridiculous. What am I doing wrong?
results in an empty array even though I can see the data exists in neon db through the web client.send snippets of your schema and the code thats isnt working as expected, otherwise its hard to say
You can also put it together on https://drizzle.run/ and see if it works there. This way we can also test is. I haven't worked with neon, but it seems to be just host postgres, so you should be able to put your code into the playground without any issues.
Drizzle Run
Drizzle Run
my schema
my seeding script
this prints empty array
I cannot get the rows after I have inserted them
this was causing foreign key conflict cause the orgId I was referencing in memberships table wasn't even present in the orgs table
the leftover part of the seeding script
Oh you await in the array you pass to vslues
pardon?
You have async code for your hashed password
would that be problematic?
So you have to await all in a Promise.all()
I don't have any such calls in the array when inserting in orgs though
still that fails
prints
insertedUsers []
wait
same output even after removing awaits from the array inside Promise.allI can see it in the neon table explorer
and for some reason the id keeps starting from the incremented value instead of 0
even tho I delete those records
That’s expected , serial will always increment and sometimes could skip some numbers
Would you mind trying again but within a db.transaction?
I tried but the neon client doesn't support transactions
Ah 😌
I got an error along those lines
got the issue?
still, I would expect each query statement to be auto committed
It should be but insert like that (across multiple tables) should be in a transaction (insert all or nothing). I’m checking about neon and transactions. Maybe I missed something
huh
What driver do you use? Neon serverless or Postgres?
neon serverless (
"@neondatabase/serverless": "^0.9.5"
)
sorry for the ping 😐Transaction should work (it’s in their doc) 🧐
Error: No transactions support in neon-http driver
I get this errornvm, got it I think https://orm.drizzle.team/docs/get-started-postgresql#neon-postgres
Drizzle ORM - PostgreSQL
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
websocket 👍
I missed that somehow
Ok that’s over http for serverless env. that can explain why you see empty data
This is why I don’t use serverless envs 😅 looks complicated
yay, fixed
too late to grasp the issue properly though
I have a few questions though:
but insert like that (across multiple tables) should be in a transaction (insert all or nothing)Are DB commits not guaranteed to be as expected across tables? wdym by http for serverless env? why does it differ from websocket? good night 😴