diff --git a/data/database.py b/data/database.py index 00735b49b..dd4a2eb77 100644 --- a/data/database.py +++ b/data/database.py @@ -552,6 +552,9 @@ class UserRegion(BaseModel): ) +_ImageProxy = Proxy() + + class Image(BaseModel): # This class is intentionally denormalized. Even though images are supposed # to be globally unique we can't treat them as such for permissions and @@ -574,7 +577,9 @@ class Image(BaseModel): security_indexed = BooleanField(default=False) security_indexed_engine = IntegerField(default=-1) - parent_id = IntegerField(index=True, null=True) + + # We use a proxy here instead of 'self' in order to disable the foreign key constraint + parent = ForeignKeyField(_ImageProxy, index=True, null=True, related_name='children') class Meta: database = db @@ -587,6 +592,9 @@ class Image(BaseModel): ) +_ImageProxy.initialize(Image) + + class RepositoryTag(BaseModel): name = CharField() image = ForeignKeyField(Image) diff --git a/data/model/image.py b/data/model/image.py index d07c56416..943d9f383 100644 --- a/data/model/image.py +++ b/data/model/image.py @@ -227,7 +227,7 @@ def _find_or_link_image(existing_image, repo_obj, username, translations, prefer comment=existing_image.comment, v1_json_metadata=existing_image.v1_json_metadata, aggregate_size=existing_image.aggregate_size, - parent_id=translated_parent_id, + parent=translated_parent_id, v1_checksum=existing_image.v1_checksum) @@ -327,7 +327,7 @@ def set_image_metadata(docker_image_id, namespace_name, repository_name, created if parent: fetched.ancestors = '%s%s/' % (parent.ancestors, parent.id) - fetched.parent_id = parent.id + fetched.parent = parent fetched.save() return fetched @@ -429,10 +429,8 @@ def synthesize_v1_image(repo, image_storage, docker_image_id, created_date_str, specified metadata. """ ancestors = '/' - parent_id = None if parent_image is not None: ancestors = '{0}{1}/'.format(parent_image.ancestors, parent_image.id) - parent_id = parent_image.id created = None if created_date_str is not None: @@ -444,7 +442,7 @@ def synthesize_v1_image(repo, image_storage, docker_image_id, created_date_str, return Image.create(docker_image_id=docker_image_id, ancestors=ancestors, comment=comment, command=command, v1_json_metadata=v1_json_metadata, created=created, - storage=image_storage, repository=repo, parent_id=parent_id) + storage=image_storage, repository=repo, parent=parent_image) def ensure_image_locations(*names):