Monday, June 19, 2017

Non-primary auto-incrementing field with Postgres

I am working on a project that uses Django 1.10.5 with Postgres 9.6 (and Python 3.6.1).

 

I am currently migrating historical data from an old system into the new system. This historical data has a table with a (non-primary key) “ID” column. I would like to migrate these rows into the new database, and have that column be auto-incrementing (starting from, say, 100000) for new rows inserted.

 

I think the way to do this with raw Postgres SQL would be to create a serial field, and then run a SQL command like “SELECT SETVAL('some_table_field_id_seq', 100000)”. I think.  :)

 

Does Django expose functionality that lets me accomplish this?

 

I looked at AutoField, but it doesn’t allow usage when primary_key=False.

 

T

 

No comments:

Post a Comment