fix(gallery+tests): alias Post inside artist EXISTS; tests stop asserting synthetic Source
gallery_service._provenance_clause artist branch was correlating its bare Post reference to the outer query's primary_post_id outer-join, so the artist filter silently matched zero rows for images with no primary post. Alias Post inside the EXISTS subquery so SQLAlchemy adds it to the inner FROM rather than treating it as a correlated outer table. Five sidecar/import tests still asserted that a synthetic Source row appears after a filesystem import. Alembic 0030 retired that behavior; the Post sits null-source and the artist linkage lives on Post.artist_id. Updated test_sidecar_creates_provenance, test_reimport_same_post_idempotent, test_sidecar_artist_used_when_no_folder_artist, test_supersede_applies_new_file_sidecar, and test_apply_sidecar_recovers_from_integrity_error to assert post.source_id IS NULL + post.artist_id linkage instead.
This commit is contained in:
@@ -20,6 +20,7 @@ from datetime import datetime
|
||||
|
||||
from sqlalchemy import Select, and_, exists, func, or_, select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.orm import aliased
|
||||
|
||||
from ..models import Artist, ImageProvenance, ImageRecord, Post, Tag
|
||||
from ..models.tag import image_tag
|
||||
@@ -139,10 +140,17 @@ def _provenance_clause(post_id, artist_id):
|
||||
# path now drops NULL-source provenance rows (filesystem-imported
|
||||
# content) which would otherwise vanish from artist-filtered
|
||||
# gallery views.
|
||||
# ALIAS Post: the gallery query outer-joins Post on
|
||||
# ImageRecord.primary_post_id (`_outer_join_primary_post`).
|
||||
# SQLAlchemy would otherwise correlate a bare `Post` reference
|
||||
# in this EXISTS subquery to that outer Post (which is NULL for
|
||||
# images with no primary post), and the filter would silently
|
||||
# match nothing.
|
||||
post_inner = aliased(Post)
|
||||
return exists().where(
|
||||
ImageProvenance.image_record_id == ImageRecord.id,
|
||||
ImageProvenance.post_id == Post.id,
|
||||
Post.artist_id == artist_id,
|
||||
ImageProvenance.post_id == post_inner.id,
|
||||
post_inner.artist_id == artist_id,
|
||||
)
|
||||
return None
|
||||
|
||||
|
||||
Reference in New Issue
Block a user