Hey everyone! I'm working on seeding my database (...
# orm-help
j
Hey everyone! I'm working on seeding my database (with prisma 3.7) and I can get the data to seed properly, but I'm trying to figure out how to reset all the auto-incremented sequences in my database. Most of my IDs are auto-incremented INTs and I need those to get reset each time I seed. I found some code like this `await prisma.$queryRaw`ALTER TABLE Product AUTO_INCREMENT = 1`;` but that gives me an error and I think it's because I'm using postgres. So I tried something like this `await prisma.$queryRaw`TRUNCATE User RESTART IDENTITY`;`, but unfortunately that is throwing the following error: `Raw query failed. Code:
42601
. Message: `db error: ERROR: syntax error at or near "user"`` and if I run that same query but on a different model, "budget", for example, I'm getting this error:
'relation "budget" does not exist'
. Any help would be greatly appreciated. Thanks!
1
e
Heya! Would resetting the whole database before running seed work for you? If it does, try running
prisma migrate reset
before running your seed command 🙂 Doco
j
I don't know, but I figured it out. Every time I feel bold enough to ask a question, I end up figuring it out like 10 minutes later and then am embarrassed about asking it in the first place. So thanks for the possible solution and for not making me feel stupid. It turns out the actual database tables were created is Pascal case, so I had to wrap them in double quotes because postgres was converting the unquoted table names to lowercase. I also had to truncate all of the current tables at the same time because of how they were structured with foreign keys. I ended up with `prisma.$queryRaw`truncate "Budget", "Category", "Income", "Transaction", "BudgetItem", "TransactionItem", "User" restart identity;`;` and it worked perfectly. The way I figured it out was I went directly inside the database with the command line and noticed that when I ran
select * from user;
it was giving me the database username as the return result, when I was expecting it to return my app's user record. When I ran
select * from "User";
, I got the expected result. However, I've slept since then and so I may be mistaken, but I'm almost positive that I tried running `prisma.$queryRaw`TRUNCATE "User" RESTART IDENTITY`;` and it still threw a syntax error. Anyway, when I started truncating tables directly from the database inside the command line, postgres told me to truncate them at the same time. So that's what I did, and it worked.
💯 1
e
Ah casing - the bane of every tired engineer 😄 Glad you solved your problem!