From 22286ddd0d3040d4e67ef33247e8710f9943c8d5 Mon Sep 17 00:00:00 2001 From: Alexander Hess Date: Sat, 5 Dec 2026 11:45:11 +0100 Subject: [PATCH] Make the copied columns read-only See the following commit for another approach. --- src/velofy/db/addresses_pixels.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/velofy/db/addresses_pixels.py b/src/velofy/db/addresses_pixels.py index d11b729..13f147b 100644 --- a/src/velofy/db/addresses_pixels.py +++ b/src/velofy/db/addresses_pixels.py @@ -25,11 +25,17 @@ class AddressPixelAssociation(meta.Base): sa.Integer, primary_key=True, ) - city_id: orm.Mapped[int] = orm.mapped_column( + # `sqlalchemy` copies it from the `.address` + # => Read-only `.city_id` below + _city_id: orm.Mapped[int] = orm.mapped_column( + "city_id", sa.SmallInteger, nullable=False, ) - grid_id: orm.Mapped[int] = orm.mapped_column( + # `sqlalchemy` copies it from the `.pixel` + # => Read-only `.grid_id` below + _grid_id: orm.Mapped[int] = orm.mapped_column( + "grid_id", sa.SmallInteger, nullable=False, ) @@ -77,3 +83,13 @@ class AddressPixelAssociation(meta.Base): "Pixel", back_populates="addresses", ) + + # Read-only views of the private columns above + city_id: orm.Mapped[int] = orm.synonym( + "_city_id", + descriptor=property(lambda self: self._city_id), + ) + grid_id: orm.Mapped[int] = orm.synonym( + "_grid_id", + descriptor=property(lambda self: self._grid_id), + )