Not able to update / create a Terraform state - Error 500 / duplicate key value violates unique constraint
Summary
After upgrading to Gitlab 13.7.4 it´s not possible to update the Terraform state file within a project using the Terraform HTTP backend.
Steps to reproduce
Create a Terraform project using the HTTP backend as explained in the Gitlab documentation: https://docs.gitlab.com/13.7/ee/user/infrastructure/terraform_state.html#changing-the-backend
Run terraform plan && terraform apply
What is the current bug behavior?
Terraform fails to upload the state file.
What is the expected correct behavior?
Terraform should upload the state file using the HTTP backend to the given Gitlab project.
Relevant logs and/or screenshots
Terraform DEBUG output:
2021/01/21 09:56:13 [DEBUG] GET https://mygiturl/api/v4/projects/123/terraform/state/dev 2021/01/21 09:56:13 [DEBUG] POST https://mygiturl/api/v4/projects/123/terraform/state/dev?ID=59f7f661-e4e2-85a0-7b80-1005c936234d 2021/01/21 09:56:13 [DEBUG] POST https://mygiturl/api/v4/projects/123/terraform/state/dev?ID=59f7f661-e4e2-85a0-7b80-1005c936234d (status: 500): retrying in 5s (1 left) Failed to save state: Failed to upload state: POST https://mygiturl/123/v4/projects/341/terraform/state/dev?ID=59f7f661-e4e2-85a0-7b80-1005c936234d giving up after 2 attempts
==> /var/log/gitlab/postgresql/current <==
2021-01-21_08:56:14.59643 ERROR: duplicate key value violates unique constraint "index_terraform_states_on_project_id_and_name" 2021-01-21_08:56:14.59646 DETAIL: Key (project_id, name)=(341, dev) already exists. 2021-01-21_08:56:14.59646 STATEMENT: INSERT INTO "terraform_states" ("project_id", "created_at", "updated_at", "name", "uuid") VALUES (123, '2021-01-21 08:56:14.595616', '2021-01-21 08:56:14.595616', 'dev', 'ecb5092cfcb5bc96e34822a9914ed9b5') RETURNING "id"
Results of GitLab environment info
Gitlab 13.7.4 PostgreSQL 12.4