We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
[Enhancement request] Avoid duplicate key value error on launching
Enhancement description
---------
I have these errors in log on each start:
```
postgres_1  | 2020-03-04 05:34:30.467 UTC [27] ERROR:  duplicate key value violates unique constraint "api_role_name_key"
postgres_1  | 2020-03-04 05:34:30.467 UTC [27] DETAIL:  Key (name)=(project_admin) already exists.
postgres_1  | 2020-03-04 05:34:30.467 UTC [27] STATEMENT:  INSERT INTO "api_role" ("name", "description", "created_at", "updated_at") VALUES ('project_admin', '', '2020-03-04T05:34:30.460290+00:00'::timestamptz, '2020-03-04T05:34:30.460312+00:00'::timestamptz) RETURNING "api_role"."id"
backend_1   | Datbase Error: "duplicate key value violates unique constraint "api_role_name_key"
backend_1   | DETAIL:  Key (name)=(project_admin) already exists.
backend_1   | "
postgres_1  | 2020-03-04 05:34:30.468 UTC [27] ERROR:  duplicate key value violates unique constraint "api_role_name_key"
postgres_1  | 2020-03-04 05:34:30.468 UTC [27] DETAIL:  Key (name)=(annotator) already exists.
postgres_1  | 2020-03-04 05:34:30.468 UTC [27] STATEMENT:  INSERT INTO "api_role" ("name", "description", "created_at", "updated_at") VALUES ('annotator', '', '2020-03-04T05:34:30.467909+00:00'::timestamptz, '2020-03-04T05:34:30.467926+00:00'::timestamptz) RETURNING "api_role"."id"
backend_1   | Datbase Error: "duplicate key value violates unique constraint "api_role_name_key"
backend_1   | DETAIL:  Key (name)=(annotator) already exists.
backend_1   | "
postgres_1  | 2020-03-04 05:34:30.468 UTC [27] ERROR:  duplicate key value violates unique constraint "api_role_name_key"
postgres_1  | 2020-03-04 05:34:30.468 UTC [27] DETAIL:  Key (name)=(annotation_approver) already exists.
postgres_1  | 2020-03-04 05:34:30.468 UTC [27] STATEMENT:  INSERT INTO "api_role" ("name", "description", "created_at", "updated_at") VALUES ('annotation_approver', '', '2020-03-04T05:34:30.468689+00:00'::timestamptz, '2020-03-04T05:34:30.468706+00:00'::timestamptz) RETURNING "api_role"."id"
backend_1   | Datbase Error: "duplicate key value violates unique constraint "api_role_name_key"
backend_1   | DETAIL:  Key (name)=(annotation_approver) already exists.
backend_1   | "
postgres_1  | 2020-03-04 05:34:32.026 UTC [28] ERROR:  duplicate key value violates unique constraint "auth_user_username_key"
postgres_1  | 2020-03-04 05:34:32.026 UTC [28] DETAIL:  Key (username)=(admin) already exists.
postgres_1  | 2020-03-04 05:34:32.026 UTC [28] STATEMENT:  INSERT INTO "auth_user" ("password", "last_login", "is_superuser", "username", "first_name", "last_name", "email", "is_staff", "is_active", "date_joined") VALUES ('<...>', NULL, true, 'admin', '', '', 'admin@example.com', true, true, '2020-03-04T05:34:32.023520+00:00'::timestamptz) RETURNING "auth_user"."id"
backend_1   | User admin already exists.
backend_1   | CommandError: Error: That username is already taken.
```

Propose to check existence of specified table's rows before creation to avoid these errors.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `app/server/management/commands/create_roles.py`
Content:
```
1 from api.models import Role
2 from django.core.management.base import BaseCommand
3 from django.db import DatabaseError
4 from django.conf import settings
5 
6 
7 class Command(BaseCommand):
8     help = 'Non-interactively create default roles'
9 
10     def handle(self, *args, **options):
11         try:
12             role_names = [settings.ROLE_PROJECT_ADMIN, settings.ROLE_ANNOTATOR, settings.ROLE_ANNOTATION_APPROVER]
13         except KeyError as key_error:
14             self.stderr.write(self.style.ERROR(f'Missing Key: "{key_error}"'))
15         for role_name in role_names:
16             role = Role()
17             role.name = role_name
18             try:
19                 role.save()
20             except DatabaseError as db_error:
21                 self.stderr.write(self.style.ERROR(f'Datbase Error: "{db_error}"'))
22             else:
23                 self.stdout.write(self.style.SUCCESS(f'Role created successfully "{role_name}"'))
24
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/app/server/management/commands/create_roles.py b/app/server/management/commands/create_roles.py
--- a/app/server/management/commands/create_roles.py
+++ b/app/server/management/commands/create_roles.py
@@ -13,11 +13,13 @@
         except KeyError as key_error:
             self.stderr.write(self.style.ERROR(f'Missing Key: "{key_error}"'))
         for role_name in role_names:
+            if Role.objects.filter(name=role_name).exists():
+                continue
             role = Role()
             role.name = role_name
             try:
                 role.save()
             except DatabaseError as db_error:
-                self.stderr.write(self.style.ERROR(f'Datbase Error: "{db_error}"'))
+                self.stderr.write(self.style.ERROR(f'Database Error: "{db_error}"'))
             else:
                 self.stdout.write(self.style.SUCCESS(f'Role created successfully "{role_name}"'))

{"golden_diff": "diff --git a/app/server/management/commands/create_roles.py b/app/server/management/commands/create_roles.py\n--- a/app/server/management/commands/create_roles.py\n+++ b/app/server/management/commands/create_roles.py\n@@ -13,11 +13,13 @@\n         except KeyError as key_error:\n             self.stderr.write(self.style.ERROR(f'Missing Key: \"{key_error}\"'))\n         for role_name in role_names:\n+            if Role.objects.filter(name=role_name).exists():\n+                continue\n             role = Role()\n             role.name = role_name\n             try:\n                 role.save()\n             except DatabaseError as db_error:\n-                self.stderr.write(self.style.ERROR(f'Datbase Error: \"{db_error}\"'))\n+                self.stderr.write(self.style.ERROR(f'Database Error: \"{db_error}\"'))\n             else:\n                 self.stdout.write(self.style.SUCCESS(f'Role created successfully \"{role_name}\"'))\n", "issue": "[Enhancement request] Avoid duplicate key value error on launching\nEnhancement description\r\n---------\r\nI have these errors in log on each start:\r\n```\r\npostgres_1  | 2020-03-04 05:34:30.467 UTC [27] ERROR:  duplicate key value violates unique constraint \"api_role_name_key\"\r\npostgres_1  | 2020-03-04 05:34:30.467 UTC [27] DETAIL:  Key (name)=(project_admin) already exists.\r\npostgres_1  | 2020-03-04 05:34:30.467 UTC [27] STATEMENT:  INSERT INTO \"api_role\" (\"name\", \"description\", \"created_at\", \"updated_at\") VALUES ('project_admin', '', '2020-03-04T05:34:30.460290+00:00'::timestamptz, '2020-03-04T05:34:30.460312+00:00'::timestamptz) RETURNING \"api_role\".\"id\"\r\nbackend_1   | Datbase Error: \"duplicate key value violates unique constraint \"api_role_name_key\"\r\nbackend_1   | DETAIL:  Key (name)=(project_admin) already exists.\r\nbackend_1   | \"\r\npostgres_1  | 2020-03-04 05:34:30.468 UTC [27] ERROR:  duplicate key value violates unique constraint \"api_role_name_key\"\r\npostgres_1  | 2020-03-04 05:34:30.468 UTC [27] DETAIL:  Key (name)=(annotator) already exists.\r\npostgres_1  | 2020-03-04 05:34:30.468 UTC [27] STATEMENT:  INSERT INTO \"api_role\" (\"name\", \"description\", \"created_at\", \"updated_at\") VALUES ('annotator', '', '2020-03-04T05:34:30.467909+00:00'::timestamptz, '2020-03-04T05:34:30.467926+00:00'::timestamptz) RETURNING \"api_role\".\"id\"\r\nbackend_1   | Datbase Error: \"duplicate key value violates unique constraint \"api_role_name_key\"\r\nbackend_1   | DETAIL:  Key (name)=(annotator) already exists.\r\nbackend_1   | \"\r\npostgres_1  | 2020-03-04 05:34:30.468 UTC [27] ERROR:  duplicate key value violates unique constraint \"api_role_name_key\"\r\npostgres_1  | 2020-03-04 05:34:30.468 UTC [27] DETAIL:  Key (name)=(annotation_approver) already exists.\r\npostgres_1  | 2020-03-04 05:34:30.468 UTC [27] STATEMENT:  INSERT INTO \"api_role\" (\"name\", \"description\", \"created_at\", \"updated_at\") VALUES ('annotation_approver', '', '2020-03-04T05:34:30.468689+00:00'::timestamptz, '2020-03-04T05:34:30.468706+00:00'::timestamptz) RETURNING \"api_role\".\"id\"\r\nbackend_1   | Datbase Error: \"duplicate key value violates unique constraint \"api_role_name_key\"\r\nbackend_1   | DETAIL:  Key (name)=(annotation_approver) already exists.\r\nbackend_1   | \"\r\npostgres_1  | 2020-03-04 05:34:32.026 UTC [28] ERROR:  duplicate key value violates unique constraint \"auth_user_username_key\"\r\npostgres_1  | 2020-03-04 05:34:32.026 UTC [28] DETAIL:  Key (username)=(admin) already exists.\r\npostgres_1  | 2020-03-04 05:34:32.026 UTC [28] STATEMENT:  INSERT INTO \"auth_user\" (\"password\", \"last_login\", \"is_superuser\", \"username\", \"first_name\", \"last_name\", \"email\", \"is_staff\", \"is_active\", \"date_joined\") VALUES ('<...>', NULL, true, 'admin', '', '', 'admin@example.com', true, true, '2020-03-04T05:34:32.023520+00:00'::timestamptz) RETURNING \"auth_user\".\"id\"\r\nbackend_1   | User admin already exists.\r\nbackend_1   | CommandError: Error: That username is already taken.\r\n```\r\n\r\nPropose to check existence of specified table's rows before creation to avoid these errors.\n", "before_files": [{"content": "from api.models import Role\nfrom django.core.management.base import BaseCommand\nfrom django.db import DatabaseError\nfrom django.conf import settings\n\n\nclass Command(BaseCommand):\n    help = 'Non-interactively create default roles'\n\n    def handle(self, *args, **options):\n        try:\n            role_names = [settings.ROLE_PROJECT_ADMIN, settings.ROLE_ANNOTATOR, settings.ROLE_ANNOTATION_APPROVER]\n        except KeyError as key_error:\n            self.stderr.write(self.style.ERROR(f'Missing Key: \"{key_error}\"'))\n        for role_name in role_names:\n            role = Role()\n            role.name = role_name\n            try:\n                role.save()\n            except DatabaseError as db_error:\n                self.stderr.write(self.style.ERROR(f'Datbase Error: \"{db_error}\"'))\n            else:\n                self.stdout.write(self.style.SUCCESS(f'Role created successfully \"{role_name}\"'))\n", "path": "app/server/management/commands/create_roles.py"}], "after_files": [{"content": "from api.models import Role\nfrom django.core.management.base import BaseCommand\nfrom django.db import DatabaseError\nfrom django.conf import settings\n\n\nclass Command(BaseCommand):\n    help = 'Non-interactively create default roles'\n\n    def handle(self, *args, **options):\n        try:\n            role_names = [settings.ROLE_PROJECT_ADMIN, settings.ROLE_ANNOTATOR, settings.ROLE_ANNOTATION_APPROVER]\n        except KeyError as key_error:\n            self.stderr.write(self.style.ERROR(f'Missing Key: \"{key_error}\"'))\n        for role_name in role_names:\n            if Role.objects.filter(name=role_name).exists():\n                continue\n            role = Role()\n            role.name = role_name\n            try:\n                role.save()\n            except DatabaseError as db_error:\n                self.stderr.write(self.style.ERROR(f'Database Error: \"{db_error}\"'))\n            else:\n                self.stdout.write(self.style.SUCCESS(f'Role created successfully \"{role_name}\"'))\n", "path": "app/server/management/commands/create_roles.py"}]}