views:

39

answers:

2

In order to save time moving data I pickled some models and dumped them to file. I then reloaded them into another database using the same exact model. The save worked fine and the objects kept their old id which is what I wanted. However, when saving new objects I run into nextval errors.

Not being very adept with postgres, I'm not sure how to fix this so I can keep old records with their existing ID while being able to continue adding new data.

Thanks, Thomas

+2  A: 

I think that you are talking about the sequence that is being used for autoincrementing your id fields.

the easiest solution here would be in a "psql" shell:

select max(id)+1 from YOURAPP_YOURMODEL;

and use the value in this command:

alter sequence YOURAPP_YOURMODEL_id_seq restart with MAX_ID_FROM_PREV_STATEMENT;

that should do the trick.

mawimawi
That did the trick. Thanks for the insight!
Thomas Hibbard
+3  A: 

There is actually a django command that prints out sequence reset SQL called sqlsequencereset.

$ python manage.py sqlsequencereset issues
BEGIN;
SELECT setval('"issues_project_id_seq"', coalesce(max("id"), 1), max("id") IS NOT null) FROM "issues_project";
COMMIT;
rebus