We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Database errors raised when importing content
**Version**
Main pulpcore branch. The issue arose after merging the labels refractor work (https://github.com/pulp/pulpcore/commit/4e25949176d72c5dbe1c7623a9c47d253a18b085) . 

Reproducible in pulp_file and pulp_rpm. 

**Describe the bug**
```
pulp [d32341b1-78b2-44da-b43d-e51121df9e95]: pulpcore.tasking.pulpcore_worker:INFO: Task 4c2b456b-d9a8-4238-bb45-7b63f403229c failed (Unexpected end of string
LINE 1: ...le.file', '365f08db-ac00-4e21-8abf-af0f047064cd', '{}', '', ...
                                                             ^
)
pulp [d32341b1-78b2-44da-b43d-e51121df9e95]: pulpcore.tasking.pulpcore_worker:INFO:   File "/home/vagrant/devel/pulpcore/pulpcore/tasking/pulpcore_worker.py", line 444, in _perform_task
    result = func(*args, **kwargs)
  File "/home/vagrant/devel/pulpcore/pulpcore/app/tasks/importer.py", line 236, in import_repository_version
    for a_result in _import_file(os.path.join(rv_path, filename), res_class, retry=True):
  File "/home/vagrant/devel/pulpcore/pulpcore/app/tasks/importer.py", line 138, in _import_file
    a_result = resource.import_data(data, raise_errors=True)
  File "/usr/local/lib/pulp/lib64/python3.10/site-packages/import_export/resources.py", line 819, in import_data
    return self.import_data_inner(
  File "/usr/local/lib/pulp/lib64/python3.10/site-packages/import_export/resources.py", line 871, in import_data_inner
    raise row_result.errors[-1].error
  File "/usr/local/lib/pulp/lib64/python3.10/site-packages/import_export/resources.py", line 743, in import_row
    self.save_instance(instance, new, using_transactions, dry_run)
  File "/usr/local/lib/pulp/lib64/python3.10/site-packages/import_export/resources.py", line 500, in save_instance
    instance.save()
  File "/home/vagrant/devel/pulpcore/pulpcore/app/models/repository.py", line 95, in save
    super().save(*args, **kwargs)
  File "/home/vagrant/devel/pulpcore/pulpcore/app/models/base.py", line 203, in save
    return super().save(*args, **kwargs)
  File "/usr/lib64/python3.10/contextlib.py", line 79, in inner
    return func(*args, **kwds)
  File "/usr/local/lib/pulp/lib64/python3.10/site-packages/django_lifecycle/mixins.py", line 169, in save
    save(*args, **kwargs)
  File "/usr/local/lib/pulp/lib64/python3.10/site-packages/django/db/models/base.py", line 739, in save
    self.save_base(using=using, force_insert=force_insert,
  File "/usr/local/lib/pulp/lib64/python3.10/site-packages/django/db/models/base.py", line 775, in save_base
    parent_inserted = self._save_parents(cls, using, update_fields)
  File "/usr/local/lib/pulp/lib64/python3.10/site-packages/django/db/models/base.py", line 804, in _save_parents
    updated = self._save_table(
  File "/usr/local/lib/pulp/lib64/python3.10/site-packages/django/db/models/base.py", line 881, in _save_table
    results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
  File "/usr/local/lib/pulp/lib64/python3.10/site-packages/django/db/models/base.py", line 919, in _do_insert
    return manager._insert(
  File "/usr/local/lib/pulp/lib64/python3.10/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/usr/local/lib/pulp/lib64/python3.10/site-packages/django/db/models/query.py", line 1270, in _insert
    return query.get_compiler(using=using).execute_sql(returning_fields)
  File "/usr/local/lib/pulp/lib64/python3.10/site-packages/django/db/models/sql/compiler.py", line 1416, in execute_sql
    cursor.execute(sql, params)
  File "/usr/local/lib/pulp/lib64/python3.10/site-packages/django/db/backends/utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/usr/local/lib/pulp/lib64/python3.10/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/usr/local/lib/pulp/lib64/python3.10/site-packages/django/db/backends/utils.py", line 79, in _execute
    with self.db.wrap_database_errors:
  File "/usr/local/lib/pulp/lib64/python3.10/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/usr/local/lib/pulp/lib64/python3.10/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
```

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `pulpcore/app/modelresource.py`
Content:
```
1 from import_export import fields
2 from import_export.widgets import ForeignKeyWidget
3 from logging import getLogger
4 
5 from pulpcore.app.models.content import (
6     Artifact,
7     Content,
8     ContentArtifact,
9 )
10 from pulpcore.app.models.repository import Repository
11 from pulpcore.constants import ALL_KNOWN_CONTENT_CHECKSUMS
12 from pulpcore.plugin.importexport import QueryModelResource
13 
14 
15 log = getLogger(__name__)
16 
17 
18 #
19 # Artifact and Repository are different from other import-export entities, in that they are not
20 # repo-version-specific.
21 #
22 class ArtifactResource(QueryModelResource):
23     """Resource for import/export of artifacts."""
24 
25     def before_import_row(self, row, **kwargs):
26         """
27         Sets digests to None if they are blank strings.
28 
29         Args:
30             row (tablib.Dataset row): incoming import-row representing a single Variant.
31             kwargs: args passed along from the import() call.
32 
33         """
34         # the export converts None to blank strings but sha384 and sha512 have unique constraints
35         # that get triggered if they are blank. convert checksums back into None if they are blank.
36         for checksum in ALL_KNOWN_CONTENT_CHECKSUMS:
37             if row[checksum] == "":
38                 row[checksum] = None
39 
40     class Meta:
41         model = Artifact
42         exclude = (
43             "pulp_id",
44             "pulp_created",
45             "pulp_last_updated",
46         )
47         import_id_fields = ("sha256",)
48 
49 
50 class RepositoryResource(QueryModelResource):
51     class Meta:
52         model = Repository
53         import_id_fields = ("name",)
54         exclude = (
55             "pulp_id",
56             "pulp_created",
57             "pulp_last_updated",
58             "content",
59             "next_version",
60             "repository_ptr",
61             "remote",
62         )
63 
64 
65 class ContentArtifactResource(QueryModelResource):
66     """
67     Handles import/export of the ContentArtifact model.
68 
69     ContentArtifact is different from other import-export entities because it has no 'natural key'
70     other than a pulp_id, which aren't shared across instances. We do some magic to link up
71     ContentArtifacts to their matching (already-imported) Content.
72 
73     Some plugin-models have sub-repositories. We take advantage of the content-mapping
74     machinery to account for those contentartifacts as well.
75     """
76 
77     artifact = fields.Field(
78         column_name="artifact", attribute="artifact", widget=ForeignKeyWidget(Artifact, "sha256")
79     )
80 
81     def __init__(self, repo_version=None, content_mapping=None):
82         self.content_mapping = content_mapping
83         super().__init__(repo_version)
84 
85     def before_import_row(self, row, **kwargs):
86         """
87         Fixes the content-ptr of an incoming content-artifact row at import time.
88 
89         Finds the 'original uuid' of the Content for this row, looks it up as the
90         'upstream_id' of imported Content, and then replaces the Content-pk with its
91         (new) uuid.
92 
93         Args:
94             row (tablib.Dataset row): incoming import-row representing a single ContentArtifact.
95             kwargs: args passed along from the import() call.
96 
97         Returns:
98             (tablib.Dataset row): row that now points to the new downstream uuid for its content.
99         """
100 
101         linked_content = Content.objects.get(upstream_id=row["content"])
102         row["content"] = str(linked_content.pulp_id)
103 
104     def set_up_queryset(self):
105         vers_content = ContentArtifact.objects.filter(content__in=self.repo_version.content)
106         if self.content_mapping:
107             all_content = []
108             for content_ids in self.content_mapping.values():
109                 all_content.extend(content_ids)
110             vers_content = vers_content.union(
111                 ContentArtifact.objects.filter(content__in=all_content)
112             )
113         return vers_content.order_by("content", "relative_path")
114 
115     class Meta:
116         model = ContentArtifact
117         import_id_fields = (
118             "content",
119             "relative_path",
120         )
121         exclude = (
122             "pulp_created",
123             "pulp_last_updated",
124             "_artifacts",
125             "pulp_id",
126         )
127
```
--- 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/pulpcore/app/modelresource.py b/pulpcore/app/modelresource.py
--- a/pulpcore/app/modelresource.py
+++ b/pulpcore/app/modelresource.py
@@ -59,6 +59,7 @@
             "next_version",
             "repository_ptr",
             "remote",
+            "pulp_labels",
         )

{"golden_diff": "diff --git a/pulpcore/app/modelresource.py b/pulpcore/app/modelresource.py\n--- a/pulpcore/app/modelresource.py\n+++ b/pulpcore/app/modelresource.py\n@@ -59,6 +59,7 @@\n             \"next_version\",\n             \"repository_ptr\",\n             \"remote\",\n+            \"pulp_labels\",\n         )\n", "issue": "Database errors raised when importing content\n**Version**\r\nMain pulpcore branch. The issue arose after merging the labels refractor work (https://github.com/pulp/pulpcore/commit/4e25949176d72c5dbe1c7623a9c47d253a18b085) . \r\n\r\nReproducible in pulp_file and pulp_rpm. \r\n\r\n**Describe the bug**\r\n```\r\npulp [d32341b1-78b2-44da-b43d-e51121df9e95]: pulpcore.tasking.pulpcore_worker:INFO: Task 4c2b456b-d9a8-4238-bb45-7b63f403229c failed (Unexpected end of string\r\nLINE 1: ...le.file', '365f08db-ac00-4e21-8abf-af0f047064cd', '{}', '', ...\r\n                                                             ^\r\n)\r\npulp [d32341b1-78b2-44da-b43d-e51121df9e95]: pulpcore.tasking.pulpcore_worker:INFO:   File \"/home/vagrant/devel/pulpcore/pulpcore/tasking/pulpcore_worker.py\", line 444, in _perform_task\r\n    result = func(*args, **kwargs)\r\n  File \"/home/vagrant/devel/pulpcore/pulpcore/app/tasks/importer.py\", line 236, in import_repository_version\r\n    for a_result in _import_file(os.path.join(rv_path, filename), res_class, retry=True):\r\n  File \"/home/vagrant/devel/pulpcore/pulpcore/app/tasks/importer.py\", line 138, in _import_file\r\n    a_result = resource.import_data(data, raise_errors=True)\r\n  File \"/usr/local/lib/pulp/lib64/python3.10/site-packages/import_export/resources.py\", line 819, in import_data\r\n    return self.import_data_inner(\r\n  File \"/usr/local/lib/pulp/lib64/python3.10/site-packages/import_export/resources.py\", line 871, in import_data_inner\r\n    raise row_result.errors[-1].error\r\n  File \"/usr/local/lib/pulp/lib64/python3.10/site-packages/import_export/resources.py\", line 743, in import_row\r\n    self.save_instance(instance, new, using_transactions, dry_run)\r\n  File \"/usr/local/lib/pulp/lib64/python3.10/site-packages/import_export/resources.py\", line 500, in save_instance\r\n    instance.save()\r\n  File \"/home/vagrant/devel/pulpcore/pulpcore/app/models/repository.py\", line 95, in save\r\n    super().save(*args, **kwargs)\r\n  File \"/home/vagrant/devel/pulpcore/pulpcore/app/models/base.py\", line 203, in save\r\n    return super().save(*args, **kwargs)\r\n  File \"/usr/lib64/python3.10/contextlib.py\", line 79, in inner\r\n    return func(*args, **kwds)\r\n  File \"/usr/local/lib/pulp/lib64/python3.10/site-packages/django_lifecycle/mixins.py\", line 169, in save\r\n    save(*args, **kwargs)\r\n  File \"/usr/local/lib/pulp/lib64/python3.10/site-packages/django/db/models/base.py\", line 739, in save\r\n    self.save_base(using=using, force_insert=force_insert,\r\n  File \"/usr/local/lib/pulp/lib64/python3.10/site-packages/django/db/models/base.py\", line 775, in save_base\r\n    parent_inserted = self._save_parents(cls, using, update_fields)\r\n  File \"/usr/local/lib/pulp/lib64/python3.10/site-packages/django/db/models/base.py\", line 804, in _save_parents\r\n    updated = self._save_table(\r\n  File \"/usr/local/lib/pulp/lib64/python3.10/site-packages/django/db/models/base.py\", line 881, in _save_table\r\n    results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)\r\n  File \"/usr/local/lib/pulp/lib64/python3.10/site-packages/django/db/models/base.py\", line 919, in _do_insert\r\n    return manager._insert(\r\n  File \"/usr/local/lib/pulp/lib64/python3.10/site-packages/django/db/models/manager.py\", line 85, in manager_method\r\n    return getattr(self.get_queryset(), name)(*args, **kwargs)\r\n  File \"/usr/local/lib/pulp/lib64/python3.10/site-packages/django/db/models/query.py\", line 1270, in _insert\r\n    return query.get_compiler(using=using).execute_sql(returning_fields)\r\n  File \"/usr/local/lib/pulp/lib64/python3.10/site-packages/django/db/models/sql/compiler.py\", line 1416, in execute_sql\r\n    cursor.execute(sql, params)\r\n  File \"/usr/local/lib/pulp/lib64/python3.10/site-packages/django/db/backends/utils.py\", line 66, in execute\r\n    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)\r\n  File \"/usr/local/lib/pulp/lib64/python3.10/site-packages/django/db/backends/utils.py\", line 75, in _execute_with_wrappers\r\n    return executor(sql, params, many, context)\r\n  File \"/usr/local/lib/pulp/lib64/python3.10/site-packages/django/db/backends/utils.py\", line 79, in _execute\r\n    with self.db.wrap_database_errors:\r\n  File \"/usr/local/lib/pulp/lib64/python3.10/site-packages/django/db/utils.py\", line 90, in __exit__\r\n    raise dj_exc_value.with_traceback(traceback) from exc_value\r\n  File \"/usr/local/lib/pulp/lib64/python3.10/site-packages/django/db/backends/utils.py\", line 84, in _execute\r\n    return self.cursor.execute(sql, params)\r\n```\n", "before_files": [{"content": "from import_export import fields\nfrom import_export.widgets import ForeignKeyWidget\nfrom logging import getLogger\n\nfrom pulpcore.app.models.content import (\n    Artifact,\n    Content,\n    ContentArtifact,\n)\nfrom pulpcore.app.models.repository import Repository\nfrom pulpcore.constants import ALL_KNOWN_CONTENT_CHECKSUMS\nfrom pulpcore.plugin.importexport import QueryModelResource\n\n\nlog = getLogger(__name__)\n\n\n#\n# Artifact and Repository are different from other import-export entities, in that they are not\n# repo-version-specific.\n#\nclass ArtifactResource(QueryModelResource):\n    \"\"\"Resource for import/export of artifacts.\"\"\"\n\n    def before_import_row(self, row, **kwargs):\n        \"\"\"\n        Sets digests to None if they are blank strings.\n\n        Args:\n            row (tablib.Dataset row): incoming import-row representing a single Variant.\n            kwargs: args passed along from the import() call.\n\n        \"\"\"\n        # the export converts None to blank strings but sha384 and sha512 have unique constraints\n        # that get triggered if they are blank. convert checksums back into None if they are blank.\n        for checksum in ALL_KNOWN_CONTENT_CHECKSUMS:\n            if row[checksum] == \"\":\n                row[checksum] = None\n\n    class Meta:\n        model = Artifact\n        exclude = (\n            \"pulp_id\",\n            \"pulp_created\",\n            \"pulp_last_updated\",\n        )\n        import_id_fields = (\"sha256\",)\n\n\nclass RepositoryResource(QueryModelResource):\n    class Meta:\n        model = Repository\n        import_id_fields = (\"name\",)\n        exclude = (\n            \"pulp_id\",\n            \"pulp_created\",\n            \"pulp_last_updated\",\n            \"content\",\n            \"next_version\",\n            \"repository_ptr\",\n            \"remote\",\n        )\n\n\nclass ContentArtifactResource(QueryModelResource):\n    \"\"\"\n    Handles import/export of the ContentArtifact model.\n\n    ContentArtifact is different from other import-export entities because it has no 'natural key'\n    other than a pulp_id, which aren't shared across instances. We do some magic to link up\n    ContentArtifacts to their matching (already-imported) Content.\n\n    Some plugin-models have sub-repositories. We take advantage of the content-mapping\n    machinery to account for those contentartifacts as well.\n    \"\"\"\n\n    artifact = fields.Field(\n        column_name=\"artifact\", attribute=\"artifact\", widget=ForeignKeyWidget(Artifact, \"sha256\")\n    )\n\n    def __init__(self, repo_version=None, content_mapping=None):\n        self.content_mapping = content_mapping\n        super().__init__(repo_version)\n\n    def before_import_row(self, row, **kwargs):\n        \"\"\"\n        Fixes the content-ptr of an incoming content-artifact row at import time.\n\n        Finds the 'original uuid' of the Content for this row, looks it up as the\n        'upstream_id' of imported Content, and then replaces the Content-pk with its\n        (new) uuid.\n\n        Args:\n            row (tablib.Dataset row): incoming import-row representing a single ContentArtifact.\n            kwargs: args passed along from the import() call.\n\n        Returns:\n            (tablib.Dataset row): row that now points to the new downstream uuid for its content.\n        \"\"\"\n\n        linked_content = Content.objects.get(upstream_id=row[\"content\"])\n        row[\"content\"] = str(linked_content.pulp_id)\n\n    def set_up_queryset(self):\n        vers_content = ContentArtifact.objects.filter(content__in=self.repo_version.content)\n        if self.content_mapping:\n            all_content = []\n            for content_ids in self.content_mapping.values():\n                all_content.extend(content_ids)\n            vers_content = vers_content.union(\n                ContentArtifact.objects.filter(content__in=all_content)\n            )\n        return vers_content.order_by(\"content\", \"relative_path\")\n\n    class Meta:\n        model = ContentArtifact\n        import_id_fields = (\n            \"content\",\n            \"relative_path\",\n        )\n        exclude = (\n            \"pulp_created\",\n            \"pulp_last_updated\",\n            \"_artifacts\",\n            \"pulp_id\",\n        )\n", "path": "pulpcore/app/modelresource.py"}], "after_files": [{"content": "from import_export import fields\nfrom import_export.widgets import ForeignKeyWidget\nfrom logging import getLogger\n\nfrom pulpcore.app.models.content import (\n    Artifact,\n    Content,\n    ContentArtifact,\n)\nfrom pulpcore.app.models.repository import Repository\nfrom pulpcore.constants import ALL_KNOWN_CONTENT_CHECKSUMS\nfrom pulpcore.plugin.importexport import QueryModelResource\n\n\nlog = getLogger(__name__)\n\n\n#\n# Artifact and Repository are different from other import-export entities, in that they are not\n# repo-version-specific.\n#\nclass ArtifactResource(QueryModelResource):\n    \"\"\"Resource for import/export of artifacts.\"\"\"\n\n    def before_import_row(self, row, **kwargs):\n        \"\"\"\n        Sets digests to None if they are blank strings.\n\n        Args:\n            row (tablib.Dataset row): incoming import-row representing a single Variant.\n            kwargs: args passed along from the import() call.\n\n        \"\"\"\n        # the export converts None to blank strings but sha384 and sha512 have unique constraints\n        # that get triggered if they are blank. convert checksums back into None if they are blank.\n        for checksum in ALL_KNOWN_CONTENT_CHECKSUMS:\n            if row[checksum] == \"\":\n                row[checksum] = None\n\n    class Meta:\n        model = Artifact\n        exclude = (\n            \"pulp_id\",\n            \"pulp_created\",\n            \"pulp_last_updated\",\n        )\n        import_id_fields = (\"sha256\",)\n\n\nclass RepositoryResource(QueryModelResource):\n    class Meta:\n        model = Repository\n        import_id_fields = (\"name\",)\n        exclude = (\n            \"pulp_id\",\n            \"pulp_created\",\n            \"pulp_last_updated\",\n            \"content\",\n            \"next_version\",\n            \"repository_ptr\",\n            \"remote\",\n            \"pulp_labels\",\n        )\n\n\nclass ContentArtifactResource(QueryModelResource):\n    \"\"\"\n    Handles import/export of the ContentArtifact model.\n\n    ContentArtifact is different from other import-export entities because it has no 'natural key'\n    other than a pulp_id, which aren't shared across instances. We do some magic to link up\n    ContentArtifacts to their matching (already-imported) Content.\n\n    Some plugin-models have sub-repositories. We take advantage of the content-mapping\n    machinery to account for those contentartifacts as well.\n    \"\"\"\n\n    artifact = fields.Field(\n        column_name=\"artifact\", attribute=\"artifact\", widget=ForeignKeyWidget(Artifact, \"sha256\")\n    )\n\n    def __init__(self, repo_version=None, content_mapping=None):\n        self.content_mapping = content_mapping\n        super().__init__(repo_version)\n\n    def before_import_row(self, row, **kwargs):\n        \"\"\"\n        Fixes the content-ptr of an incoming content-artifact row at import time.\n\n        Finds the 'original uuid' of the Content for this row, looks it up as the\n        'upstream_id' of imported Content, and then replaces the Content-pk with its\n        (new) uuid.\n\n        Args:\n            row (tablib.Dataset row): incoming import-row representing a single ContentArtifact.\n            kwargs: args passed along from the import() call.\n\n        Returns:\n            (tablib.Dataset row): row that now points to the new downstream uuid for its content.\n        \"\"\"\n\n        linked_content = Content.objects.get(upstream_id=row[\"content\"])\n        row[\"content\"] = str(linked_content.pulp_id)\n\n    def set_up_queryset(self):\n        vers_content = ContentArtifact.objects.filter(content__in=self.repo_version.content)\n        if self.content_mapping:\n            all_content = []\n            for content_ids in self.content_mapping.values():\n                all_content.extend(content_ids)\n            vers_content = vers_content.union(\n                ContentArtifact.objects.filter(content__in=all_content)\n            )\n        return vers_content.order_by(\"content\", \"relative_path\")\n\n    class Meta:\n        model = ContentArtifact\n        import_id_fields = (\n            \"content\",\n            \"relative_path\",\n        )\n        exclude = (\n            \"pulp_created\",\n            \"pulp_last_updated\",\n            \"_artifacts\",\n            \"pulp_id\",\n        )\n", "path": "pulpcore/app/modelresource.py"}]}