After importing data in PostgreSQL, duplicate key value violates unique constraint

后端 未结 4 1024
忘了有多久
忘了有多久 2021-02-02 17:51

I recently migrated my rails app to PostgreSQL in order to take advantage of fulltext search.

Since the migration coincided with moving to a new webhost, the steps for m

4条回答
  •  南旧
    南旧 (楼主)
    2021-02-02 18:16

    If the schema contains serial or sequence columns, you should reset these to the max value that occurs in the corresponding column. (normally you should not import the serials from a file, but give them the freedom to autoincrement.)

    For all imported tables you should identify the sequence fields and run the following code on them. (substitute your schema name for "sch", your table name for "mytable" and your id column name for "id")

    WITH mx AS ( SELECT MAX(id) AS id FROM sch.mytable)
    SELECT setval('sch.mytable_id_seq', mx.id) AS curseq
    FROM mx
            ;
    

提交回复
热议问题