|
| 1 | +# Copyright 2025 Google LLC All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import datetime |
| 16 | +import uuid |
| 17 | + |
| 18 | +from sqlalchemy import create_engine |
| 19 | +from sqlalchemy.orm import Session |
| 20 | + |
| 21 | +from sample_helper import run_sample |
| 22 | +from model import Singer, Concert, Venue, TicketSale |
| 23 | + |
| 24 | + |
| 25 | +# Shows how to create a non-enforced foreign key. |
| 26 | +# |
| 27 | +# The TicketSale model contains two foreign keys that are not enforced by Spanner. |
| 28 | +# This allows the related records to be deleted without the need to delete the |
| 29 | +# corresponding TicketSale record. |
| 30 | +# |
| 31 | +# __table_args__ = ( |
| 32 | +# ForeignKeyConstraint( |
| 33 | +# ["venue_code", "start_time", "singer_id"], |
| 34 | +# ["concerts.venue_code", "concerts.start_time", "concerts.singer_id"], |
| 35 | +# spanner_not_enforced=True, |
| 36 | +# ), |
| 37 | +# ) |
| 38 | +# singer_id: Mapped[str] = mapped_column(String(36), ForeignKey("singers.id", spanner_not_enforced=True)) |
| 39 | +# |
| 40 | +# See https://cloud.google.com/spanner/docs/foreign-keys/overview#informational-foreign-keys |
| 41 | +# for more information on informational foreign key constrains. |
| 42 | +def informational_fk_sample(): |
| 43 | + engine = create_engine( |
| 44 | + "spanner:///projects/sample-project/" |
| 45 | + "instances/sample-instance/" |
| 46 | + "databases/sample-database", |
| 47 | + echo=True, |
| 48 | + ) |
| 49 | + # First create a singer, venue, concert and ticket_sale. |
| 50 | + singer_id = str(uuid.uuid4()) |
| 51 | + ticket_sale_id = None |
| 52 | + with Session(engine) as session: |
| 53 | + singer = Singer(id=singer_id, first_name="John", last_name="Doe") |
| 54 | + venue = Venue(code="CH", name="Concert Hall", active=True) |
| 55 | + concert = Concert( |
| 56 | + venue=venue, |
| 57 | + start_time=datetime.datetime(2024, 11, 7, 19, 30, 0), |
| 58 | + singer=singer, |
| 59 | + title="John Doe - Live in Concert Hall", |
| 60 | + ) |
| 61 | + ticket_sale = TicketSale( |
| 62 | + concert=concert, customer_name="Alice Doe", seats=["A010", "A011", "A012"] |
| 63 | + ) |
| 64 | + session.add_all([singer, venue, concert, ticket_sale]) |
| 65 | + session.commit() |
| 66 | + ticket_sale_id = ticket_sale.id |
| 67 | + |
| 68 | + # Now delete both the singer and concert that are referenced by the ticket_sale record. |
| 69 | + # This is possible as the foreign key constraints between ticket_sales and singers/concerts |
| 70 | + # are not enforced. |
| 71 | + with Session(engine) as session: |
| 72 | + session.delete(concert) |
| 73 | + session.delete(singer) |
| 74 | + session.commit() |
| 75 | + |
| 76 | + # Verify that the ticket_sale record still exists, while the concert and singer have been |
| 77 | + # deleted. |
| 78 | + with Session(engine) as session: |
| 79 | + ticket_sale = session.get(TicketSale, ticket_sale_id) |
| 80 | + singer = session.get(Singer, singer_id) |
| 81 | + concert = session.get( |
| 82 | + Concert, ("CH", datetime.datetime(2024, 11, 7, 19, 30, 0), singer_id) |
| 83 | + ) |
| 84 | + print( |
| 85 | + "Ticket sale found: {}\nSinger found: {}\nConcert found: {}\n".format( |
| 86 | + ticket_sale is not None, singer is not None, concert is not None |
| 87 | + ) |
| 88 | + ) |
| 89 | + |
| 90 | + |
| 91 | +if __name__ == "__main__": |
| 92 | + run_sample(informational_fk_sample) |
0 commit comments