local postgres db keeps giving error duplicate key value violates unique constraint

人盡茶涼 提交于 2019-12-05 18:02:40

You need to run ANALYZE to get the row count in sync. In pgAdmin right click the table and choose "Maintenance" for that. Then press F5 on the table.

Doesn't have anything to do with the unique key violation, though. That means a value you try to enter in a column with a UNIQUE or PRIMARY KEY constraint is already present in another row.

In case of PostgreSQL unique key violation error messages, ActiveRecord::Base.connection.reset_pk_sequence!('table_name') might help to bring the key back in sync.

A common cause of this is that you've put data into a table with a primary key that's normally provided by a serial type, but the sequence behind the default() isn't in sync with the table.

actually i think the problem is not related to postgres at all. It was a combination of simultaneous duplicate ajax calls creating what would be a duplicate record, then neither are inserted.

I faced this problem in my development environment. This worked for me phppgadmin->admin->reindex. My solution is dangerous in some situations as it updates whole database.I suggest to look other solutions as well when working in production environment. However, Analyse is a good way to start as suggested by @Erwin Brandstetter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!