0

I am trying to populate some tables using data that I extracted from Google BigQuery. For that purpose I essentially normalized a flattened table into multiple tables that include the primary key of each row in the multiple tables. The important point is that I need to load those primary keys in order to satisfy foreign key references.

Having inserted this data into tables, I then try to add new rows to these tables. I don't specify the primary key, presuming that Postgres will auto-generate those key values.

However, I always get a 'duplicate key value violates unique constraint "xxx_pkey" ' type error, e.g.

"..duplicate key value violates unique constraint "collection_pkey" DETAIL: Key (id)=(1) already exists.

It seems this is triggered by including the primary key in the data when initializing table. That is, explicitly setting primary keys, somehow seems to disable or reset the expected autogeneration of the primary key. I.E. I was expecting that new rows would be assigned primary keys starting from the highest value already in a table.

Interestingly I get the same error whether I try to add a row via SQLAlchemy or from the psql console.

So, is this as expected? And if so, is there some way to get the system to again auto-generate keys? There must be some hidden psql state that controls this...the schema is unchanged by directly inserting keys, but psql behavior is changed by that action.

I am happy to provide additional information.

Thanks

bcli4d
  • 51
  • 1
  • 3
  • Yes, this is expected. If you provide a value, Postgres will not automagically adjust the sequence behind your `identity` or `serial` column. https://stackoverflow.com/questions/244243 –  Feb 12 '21 at 16:07
  • @a_horse_with_no_name, that answers my question. – bcli4d Feb 12 '21 at 17:43

0 Answers0