1

I am using Django with Postgresql. With sqlite, when I delete all the objects, and insert new ones, pk is reset to 1. However, with Postgresql, pk keeps adding up. I came across to this post (1). The accepted answer calls for:

python manage.py sqlsequencereset myapp1 myapp2 myapp3| psql

My app is products. But I don't know what to write on the right side of the pipe.

Here is what I tried:

# products is my app name
python manage.py sqlsequencereset products

The following message appeared:

BEGIN;
SELECT setval(pg_get_serial_sequence('"products_category"','id'), coalesce(max("id"), 1), max("id") IS NOT null) FROM "products_category";
SELECT setval(pg_get_serial_sequence('"products_product_categories"','id'), coalesce(max("id"), 1), max("id") IS NOT null) FROM "products_product_categories";
SELECT setval(pg_get_serial_sequence('"products_product"','id'), coalesce(max("id"), 1), max("id") IS NOT null) FROM "products_product";
COMMIT;

I then tried:

python manage.py sqlsequencereset products | psql

Here is the error message:

-bash: psql: command not found
Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>
BrokenPipeError: [Errno 32] Broken pipe

What should I put after the | ?

ha-neul
  • 3,058
  • 9
  • 24
  • related: https://stackoverflow.com/q/43893144, https://stackoverflow.com/q/51504144 – djvg Jul 11 '23 at 11:41

1 Answers1

2

Are you on windows or something ?

The python manage.py sqlsequencereset products generate the SQL code that you need to execute to reset the indexes. In the original post, the idea was to "pass" that sql code to psql so that it will be executed (that's why you have a pipe operator |) but you don't need it, you only need to execute the sql.

Do you have pgadmin ? If yes, simply use it to run the sql code generated and your indexes will be reset. Or simply drop and recreate the db. Can I ask why you want to reset the indexes?

  • that makes sense! I pasted the resulting sql code into pgadmin->tools->Query Tool-> Query Editor, and hit the run button. pgadmin says it query returned successfully. But when I check my table, the pk/id has not changed. do you know any reason why? Thanks! – ha-neul Nov 19 '20 at 22:08